在QTP中使用Vbscr
ipt來(lái)做文件處理時(shí)我們往往會(huì)使用FSO組件來(lái)進(jìn)行操作,一般我們會(huì)使用此組件來(lái)用作日志和結(jié)果的輸出,此處的對(duì)象創(chuàng)建方式為Scripting.filesystemobject, 下面列舉常用的一些操作:
如何新建一個(gè)文本文件?
如何檢查文件是否存在?
如何在文件中寫(xiě)入內(nèi)容?
如何讀取文件中的行?
如何讀取文件中完整的內(nèi)容?
如何關(guān)閉已經(jīng)打開(kāi)的文件?
如何拷貝文件?
如何刪除文件?
如何創(chuàng)建文件夾?
如何獲取文件內(nèi)容的行數(shù)?
腳本實(shí)例:
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")