Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FSO:
? File system object is an interface between QTP and the local system. using FSO
we can create/delete folder,create/delete/read
from/write to text files
? The FileSystemObject (FSO) object model allows you to use the familiar
object.method syntax with a rich set of properties,
methods, and events to process folders and files
Object/Collection Description:
FileSystemObject:
File system object is a 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:
Drive is a 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:
Drives are 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:
File is a 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:
Files are Collection. Provides a list of all files contained within a folder.
Folder:
Folder is a 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:
Folders are Collection. Provides a list of all the folders within a Folder.
TextStream:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
?Method: CreateTextFile
Description: Creates a specified file name and returns a TextStream object that can
be used to read from or write to the file
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated
filename: (Required) A string expression that identifies the file to create
overwrite: (Optional) Boolean value that indicates whether you can overwrite an
existing file. The value is True if the file
can be overwritten, False if it can't be overwritten. If omitted, existing files
are not overwritten (default False).
unicode: (Optional) Boolean value that indicates whether the file is created as
a Unicode or ASCII file. If the value is True, the file is created as a Unicode
file. If the value is False, the file is created as an ASCII file. If omitted, an
ASCII file is assumed.
Example:
'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a non existing file "qtptest.txt " with overwrite option as True
Set qfile1=fso.CreateTextFile("C:\qtptest.txt",True,False)
'Output --> New File "qtptest.txt " is created
Example:
'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a file "qtptest.txt " in C Drive .
'Then run the below statement with overwrite option as False
'Output --> Error message "Fie already exists" is displayed
Set qfile2=fso.CreateTextFile("C:\qtpexist.txt",False,False)
Set fso=nothing
?Method: CopyFile
Description: Copies one or more files from one location to a new location
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
source: (Required) A character string file specification, which can include
wildcard characters, for one or more files to be copied.
destination: (Required) Character string destination where the file or files
from source are to be copied.Wildcard characters are not allowed in the destination
string.
overwrite: (Optional) Boolean value that indicates if existing files are to be
overwritten. If True, files are overwritten; if False, they are not. The default is
True. Note that CopyFile will fail if destination has the read-only attribute set,
regardless of the value of overwrite.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'If the destination doesnot exist then create the destination folder
fso.CopyFile Sourcefile,Destination,True
Set fso=nothing
?Method: DeleteFile
Arguments:
fso: (Required) The name of a FileSystemObject object previously
instantiated
filename: (Required) The name of the file to delete. The filename can contain
wildcard characters in the last path component.
force: (Optional) Boolean value that is True of files with the read-only
attribute set are to be deleted;False if they are not. False is the default.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'File to be deleted. Sourcefile="C:\copy.txt" 'Delete the file
fso.DeleteFile Sourcefile
Set fso=nothing
?Method: CreateFolder
Syntax: fso.CreateFolder(foldername)
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
foldername: (Required) A character string expression that identifies the
folder to create.
Example:
Set fso=createobject("Scripting.FileSystemObject")
fso.CreateFolder (Foldername)
End If
Set fso=nothing
?Method: CopyFolder
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
source: (Required) A character string folder specification, which can include
wildcard characters, for one or more folders to be copied. Wildcard characters can
only be used in the last path component of the source argument.
destination: (Required) Character string destination where the folder and
subfolders from source are to be copied. Wildcard characters are not allowed in the
destination string.
overwrite: (Optional) Boolean value that indicates if existing folders are to be
overwritten. If True, files are overwritten; if False, they are not. The default is
True.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Folder to be created SourcePath="D:\Folder_create"
DestinationPath="D:\Destination\"
'If the folder doesnot exst then create the folder
fso.CopyFolder SourcePath,DestinationPath,True
Set fso=nothing
?Method: MoveFolder
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
source: (Required) The path to the folder or folders to be moved. The source
argument string can contain wildcard charactersin the last path component only.
destination: (Required) The path where the folder or folders are to be moved.
The destination argument can't contain wildcard characters.
Example:
Set fso=createobject("Scripting.FileSystemObject")
fso.CreateFolder (DestinationPath)
End If
fso.MoveFolder SourcePath,DestinationPath
Set fso=nothing
?Method: DeleteFolder
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated
folderspec: (Required) The name of the folder to delete. The folderspec can
contain wildcard characters in the last path component.
force: (Optional) Boolean value that is True of folders with the read-only
attribute set are to be deleted;False if they are not. False is the default.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Folder to be deleted. FolderDel="D:\final1" 'Delete the folder
fso.DeleteFolder(FolderDel)
Set fso=nothing
?Method: DriveExists
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
drivespec: (Required) A drive letter or a complete path specification.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'The drive to check the existence
drivepath="D:\"
If fso.DriveExists(drivepath) then
msgbox "Drive Exists" Else Msgbox "Drive doesnot Exist"
End If
Set fso=nothing
?Method: FileExists
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
filespec: (Required) The name of the file whose existence is to be determined. A
complete path specification (either absolute or relative) must be provided if the
file isn't expected to exist in the current folder.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'The file to check the existence
filepath="D:\qtptest.txt"
If fso.FileExists(filepath) then
msgbox "File Exists"
Else
Msgbox "File doesnot Exist"
End If
Set fso=nothing
?Method: FolderExists
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
folderspec: (Required) The name of the folder whose existence is to be
determined. A complete path specification (either absolute or relative) must be
provided if the folder isn't expected to exist in the current folder.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'The Folder to check the existence
folderpath="D:\qtp"
If fso.FolderExists(folderpath) then
msgbox "Folder Exists"
Else
Msgbox "Folder doesnot Exist"
End If
Set fso=nothing
?Method: GetDrive
Description: Returns a Drive object corresponding to the drive for a specified path
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
drivespec: (Required) The drivespec argument can be a drive letter (c), a drive
letter with a colon appended (c:), a drive letter with a colon and path separator
appended (c:\), or any network share specification (\\computer2\share1).
Example:
Set fso=createobject("Scripting.FileSystemObject")
Set fso=nothing
?Method: GetFolder
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
folderSpec: (Required) The folderspec is the path (absolute or relative) to a
specific folder.
Example:
Set fso=createobject("Scripting.FileSystemObject")
Next
Set fso=nothing
?Method: GetFile
Description: Returns a File object corresponding to the file in the specified path.
The file object methods and properties can be accessed. See File Object for the
file objects methods and properties.
Arguments:
fso: (Required) The name of a FileSystemObject object previously instantiated.
fileSpec: (Required) The filespec is the path (absolute or relative) to a
specific file.
Example:
Set fso=createobject("Scripting.FileSystemObject")
Set fso=nothing
?Method: Close
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
?Method: Read
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
numCharacters: (Required) The number of characters you want to read from the
file
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read characters from the file
Msgbox qfile.Read(10) 'Output --> "Welcome to" will be read
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
?Method: ReadAll
Description: Reads the entire TextStream file and returns the resulting string.
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of priously written file
?Method: ReadLine
Description: Reads an entire line (up to, but not including, the newline character)
from a TextStream file and returns the resulting string.
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of priously written file
Do while qfile.AtEndOfStream <> true
Msgbox qfile.ReadLine 'Output --> The file will be read line line by line.
Loop
'Close the files
qfile.Close
?Method: Write:
Syntax: objTso.Write(string)
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
string: (Required) The text you want to write to the file.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into a single line
qfile.Write "Welcome to the World of QTP"
qfile.Write "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of priously written file
Do while qfile.AtEndOfStream <> true
'Output --> The file will contain the above written content in single line.
Msgbox qfile.ReadLine
Loop
?Method: WriteLine
Syntax: objTso.WriteLine([string])
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
string: (Optional) The text you want to write to the file.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of priously written file
Do while qfile.AtEndOfStream <> true
'Output --> The file will contain the above written content line by line.
Msgbox qfile.ReadLine
Loop
?Method: WriteBlankLines
Syntax: objTso.WriteBlankLines(numLines)
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
numLines: (Required) The number of newline characters you want to write to the
file.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
'will insert 4 new blank lines
qfile.WriteBlankLines(4)
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of priously written file
Do while qfile.AtEndOfStream <> true
'Output --> The file will be read file line by line.
Msgbox qfile.ReadLine
Loop
?Property: AtEndOfLine
Syntax: objTso.AtEndOfLine
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into newline
qfile.Writeline "Welcome to the World of QTP"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
Do while qfile.AtEndOfLine <> true
Msgbox qfile.Read(1) 'Output --> The file will be read word by word.
Loop
?Property: AtEndOfStream
Syntax: objTso.AtEndOfStream
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.
Example:
Set fso=createobject("Scripting.FileSystemObject")
'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline "Welcome to the World of QTP"
qfile.Writeline "the file name is qtptest.txt"
'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read the entire contents of previously written file
Do while qfile.AtEndOfStream <> true
Msgbox qfile.ReadLine 'Output --> The file will be read line line by line.
Loop