qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

QTP自动化测试-TXT文件操作

 创建文本文件
  Set bjFSO = CreateObject("Scripting.FileSystemObject")
  Set bjFile = objFSO.CreateTextFile("C:\FSOScriptLog.txt")
  检察文件是否存在
Set bjFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\FSOScriptLog.txt") Then
Set bjFolder = objFSO.GetFile("C:\FSOScriptLog.txt")
Else
MsgBox "File does not exist."
End If
  删除文本文件
  Set bjFSO = CreateObject("Scripting.FileSystemObject")
  objFSO.DeleteFile("C:\FSOScriptLog.txt")
  移动文件
  Set bjFSO = CreateObject("Scripting.FileSystemObject")
  objFSO.MoveFile "C:\FSOScriptLog.txt" , "D:\"
  复制文件
  Set bjFSO = CreateObject("Scripting.FileSystemObject")
  objFSO.CopyFile "C:\FSOScriptLog.txt" , "D:\"
  重命名文件
  Set bjFSO = CreateObject("Scripting.FileSystemObject")
  objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"
  读取全部内容
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.OpenTextFile("C:ScriptsTest.txt", ForReading)
strContents = objFile.ReadAll
Wscript.Echo strContents
objFile.Close
  一行行的读取文本文件内容
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile ("C: \ScriptsTest.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.ReadLine
Wscript.Echo strComputer
Loop
objTextFile.Close
 追加文本文件一行内容
Const ForAppending = 8
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile ("C:\ScriptsTest.txt ", ForAppending, True)
objTextFile.WriteLine("追加的内容")
objTextFile.Close
  有用的几个函数:
  替换:将Jim替换成James。
  strNewText = Replace(strText, "Jim ", "James ")
  用逗号分隔字符串:
  arrpath=split(strDN,",")
  wscript.echo arrpath(0)
  读取文本文件指定的行内容(读第四行内容存到strLine变量中)
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile("C:\ScriptsTest.txt ", ForReading)
For i = 1 to 3
objTextFile.ReadLine
Next
strLine = objTextFile.Readline
MsgBox strLine
objTextFile.Close
  查看文件属性
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.GetFile("c:\ScriptsTest.txt")
msgbox "Date created: " & objFile.DateCreated
msgbox "Date last accessed: " & objFile.DateLastAccessed
msgbox "Date last modified: " & objFile.DateLastModified
msgbox "Drive: " & objFile.Drive
msgbox "Name: " & objFile.Name
msgbox "Parent folder: " & objFile.ParentFolder
msgbox "Path: " & objFile.Path
msgbox "Short name: " & objFile.ShortName
msgbox "Short path: " & objFile.ShortPath
msgbox "Size: " & objFile.Size
msgbox "Type: " & objFile.Type
  修改文件属性
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.GetFile("C:\ScriptsTest.txt")
If objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes XOR 1
End If
  写入文本文件
Const ForWriting=2
Set bj=createobject("scripting.filesystemobject")
Set bjfile=obj.opentextfile("C:\ScriptsTest.txt", ForWriting)
objfile.write("This is line 1.")
objfile.writeline("This is line2.")
objfile.close
版权声明:本文出自 wpt1987 的51Testing软件测试博客:http://www.51testing.com/?365260

posted on 2014-04-15 10:55 顺其自然EVO 阅读(681) 评论(0)  编辑  收藏 所属分类: qtp


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(54)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜