在QTP中使用Vbscr
ipt来做文件处理时我们往往会使用FSO组件来进行操作,一般我们会使用此组件来用作日志和结果的输出,此处的对象创建方式为Scripting.filesystemobject, 下面列举常用的一些操作:
如何新建一个文本文件?
如何检查文件是否存在?
如何在文件中写入内容?
如何读取文件中的行?
如何读取文件中完整的内容?
如何关闭已经打开的文件?
如何拷贝文件?
如何删除文件?
如何创建文件夹?
如何获取文件内容的行数?
脚本实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Dim objFso
' creating the file system object
Set objFso = CreateObject ("Scripting.FileSystemObject")
' Create a new txt file
' Parameters:
' FilePath - location of the file and its name
Function CreateFile (StrFilePath)
' variable that will hold the new file object
Dim NewFile
' create the new text File
Set NewFile = objFso.CreateTextFile(StrFilePath, True)
Set CreateFile = NewFile
End Function
' Check if a specific file exist
' Parameters:
' FilePath - location of the file and its name
Function CheckFileExists (StrFilePath)
' check if file exist
CheckFileExists = objFso.FileExists(StrFilePath)
End Function
' Write data to file
' Parameters:
' FileRef - reference to the file
' str - data to be written to the file
Function WriteToFile (byref FileRef,str)
' write str to the text file
FileRef.WriteLine(str)
End Function
' Read line from file
' Parameters:
' FileRef - reference to the file
Function ReadLineFromFile (byref FileRef)
' read line from text file
ReadLineFromFile = FileRef.ReadLine
End Function
' Read Entire Text File Test
' Parameters:
' FileRef - reference to the file
Function ReadTextFileTest(StrFilePath)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(StrFilePath, ForReading)
ReadTextFileTest = f.Readall()
End Function
' Closes an open file.
' Parameters:
' FileRef - reference to the file
Function CloseFile (byref FileRef)
FileRef.close
End Function
' Opens a specified file and returns an object
' that can be used to read from, write to, or append to the file.
' Parameters:
' FilePath - location of the file and its name
' mode options are:
' ForReading - 1
' ForWriting - 2
' ForAppending - 8
Function OpenFile (StrFilePath,mode)
' open the txt file and retunr the File object
set OpenFile = objFso.OpenTextFile(StrFilePath, mode, True)
End Function
' Copy file.
' Parameters:
' FileRef - reference to the file
Sub FileCopy ( StrFilePathSource,StrFilePathDest)
' copy source file to destination file
objFso.CopyFile StrFilePathSource, StrFilePathDest
End Function
' Delete a file.
' Parameters:
' FileRef - reference to the file
Sub FileDelete ( StrFilePath)
' copy source file to destination file
objFso.DeleteFile ( StrFilePath)
End Function
' Create Folder if Not Exists.
Function ReportFolderStatus(fldr)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If Not (fso.FolderExists(fldr)) Then
Set f = fso.CreateFolder(fldr)
End If
End Function
ReportFolderStatus("C:\iquicktest")
' Count Number of Lines in txt file
Function NumberOfLines(FileName)
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)
Do Until objTextFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objTextFile.ReadLine
i = i +1
Loop
NumberOfLines = UBound(arrFileLines)
objTextFile.Close
End Function
msgbox NumberOfLines("C:\iquicktest\vbs.txt")