FileSystemObject对象

FileSystemObject

Provides access to a computer's file system.

 

FileSystemObject对象(简称FSO对象)提供给了从VBS或QTP访问文件的能力,在QTP自动化测试过程中经常使用。

通常用CreateObject来创建FSO对象。

FSO对象常用的方法有:
OpenTextFile - 打开文本文件
ReadLine - 读入一行
CreateTextFile - 创建文本文件
WriteLine - 写入一行

下面是一个读取文件的例子:
Function ReadLineTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("c:/testfile.txt", ForWriting, True)
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
Set MyFile = fso.OpenTextFile("c:/testfile.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"
End Function

当然,除了文件读写外,FSO对象还可以对文件夹、目录、文件属性等进行操作:

The FileSystemObject (FSO) object model contains the following objects and collections.

Object/Collection Description
FileSystemObject Main object. Contains methods and properties that allow you to create, delete, gain information about, and generally manipulate drives, folders, and files. Many of the methods associated with this object duplicate those in other FSO objects; they are provided for convenience.
Drive Object. Contains methods and properties that allow you to gather information about a drive attached to the system, such as its share name and how much room is available. Note that a "drive" isn't necessarily a hard disk, but can be a CD-ROM drive, a RAM disk, and so forth. A drive doesn't need to be physically attached to the system; it can be also be logically connected through a network.
Drives Collection. Provides a list of the drives attached to the system, either physically or logically. The Drives collection includes all drives, regardless of type. Removable-media drives need not have media inserted for them to appear in this collection.
File Object. Contains methods and properties that allow you to create, delete, or move a file. Also allows you to query the system for a file name, path, and various other properties.
Files Collection. Provides a list of all files contained within a folder.
Folder Object. Contains methods and properties that allow you to create, delete, or move folders. Also allows you to query the system for folder names, paths, and various other properties.
Folders Collection. Provides a list of all the folders within a Folder .
TextStream Object. Allows you to read and write text files.




在QTP的安装目录,可以找到一个名为UsingFSO.vbs的文件,里面封装了常用的一些文件操作的函数,摘录如下:


dim oFSO
' creating the file system object
set oFSO = CreateObject ("Scripting.FileSystemObject")

'Option Explicit
' *********************************************************************************************
' Create a new txt file

' Parameters:
' FilePath - location of the file and its name
' *********************************************************************************************
Function CreateFile (FilePath)
' varibale that will hold the new file object
dim NewFile
' create the new text ile
set NewFile = oFSO.CreateTextFile(FilePath, True)
set CreateFile = NewFile
End Function

' *********************************************************************************************
' Check if a specific file exist

' Parameters:
' FilePath - location of the file and its name
' *********************************************************************************************
Function CheckFileExists (FilePath)
' check if file exist
CheckFileExists = oFSO.FileExists(FilePath)
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

' *********************************************************************************************
' 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 (FilePath,mode)
' open the txt file and retunr the File object
set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
End Function

' *********************************************************************************************
' Closes an open file.

' Parameters:
' FilePathSource - location of the source file and its name
' FilePathDest - location of the destination file and its name
' *********************************************************************************************
Sub FileCopy ( FilePathSource,FilePathDest)
' copy source file to destination file
oFSO.CopyFile FilePathSource, FilePathDest
End Sub

' *********************************************************************************************
' Delete a file.

' Parameters:
' FilePath - location of the file to be deleted
' *********************************************************************************************
Sub FileDelete ( FilePath)
' copy source file to destination file
oFSO.DeleteFile ( FilePath)
End Sub

' *********************************************************************************************
' Compare two text files.
'
' Parameters:
' FilePath1 - location of the first file to be compared
' FilePath2 - location of the second file to be compared
' FilePathDiff - location of the diffrences file
' ignoreWhiteSpace - controls whether or not to ignore differences in whitespace characters
' true - ignore differences in whitespace
' false - do not ignore difference in whitespace
' Return Value: true if files are identical, false otherwise'
' *********************************************************************************************
Function FileCompare (byref FilePath1, byref FilePath2, byref FilePathDiff, ignoreWhiteSpace)

dim differentFiles
differentFiles = false

dim f1, f2, f_diff
' open the files
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
set f_diff = OpenFile(FilePathDiff,8)

dim rowCountF1, rowCountF2
rowCountF1 = 0
rowCountF2 = 0

dim str
' count how many lines there are in first file
While not f1.AtEndOfStream
str = ReadLineFromFile(f1)
rowCountF1= rowCountF1 + 1
Wend

' count how many lines there are in second file
While not f2.AtEndOfStream
str = ReadLineFromFile(f2)
rowCountF2= rowCountF2 + 1
Wend

' re-open the files to go back to the first line in the files
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)

' compare the number of lines in the two files.
' assign biggerFile - the file that contain more lines
' assign smallerFile - the file that contain less lines
dim biggerFile, smallerFile
set biggerFile = f1
set smallerFile = f2
If ( rowCountF1 < rowCountF2) Then
set smallerFile = f1
set biggerFile = f2
End If

dim lineNum,str1, str2
lineNum = 1
str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2"
WriteToFile f_diff,str
' loop on all the lines in the samller file
While not smallerFile.AtEndOfStream
' read line from both files
str1 = ReadLineFromFile(f1)
str2 = ReadLineFromFile(f2)

' check if we need to ignore white spaces, if yes, trim the two lines
If Not ignoreWhiteSpace Then
Trim(str1)
Trim(str2)
End If

' if there is a diffrence between the two lines, write them to the diffrences file
If not (str1 = str2) Then
differentFiles = true
str = lineNum & vbTab & str1 & vbTab & vbTab & str2
WriteToFile f_diff,str
End If
lineNum = lineNum + 1
Wend

' loop on the bigger lines, to write its line two the diffrences file
While not biggerFile.AtEndOfStream
str1 = ReadLineFromFile(biggerFile)
str = lineNum & vbTab & "" & vbTab & vbTab & str2
WriteToFile f_diff,str
lineNum = lineNum + 1
Wend

FileCompare = Not differentFiles
End function


你可能感兴趣的:(function,properties,File,Parameters,methods,whitespace)