多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

QTP自动化测试中TXT文件操作的方法

更新时间:2022-05-10 09:35:09 作者:多测师 浏览:51

  创建文本文件

  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"

  读取全部内容

QTP自动化测试中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

  以上内容为大家介绍了QTP自动化测试中TXT文件操作的方法,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部