Académique Documents
Professionnel Documents
Culture Documents
FSO:
Filesystemobject is an object model which is used to handle the drives, folders, and files of a
system or server.
If an user needs to work on Driver, Folder, Files properties,methods or events then the first
step he need to setup is
filesystemobject
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:
TextStream is a Object. Allows you to read and write text files.
Creating a FileSystemObject Object:
First, create a FileSystemObject object by using the CreateObject method.
The following code displays how to create an instance of the FileSystemObject:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Using the Appropriate Method
Second, use the appropriate method of the FileSystemObject object. For example, to create a
new object, use either CreateTextFile
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
Syntax: Set objfile = fso.CreateTextFile(filename[, overwrite[, Unicode]])
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
'Close the files
qfile1.Close
'Release the allocated objects
Set qfile1=nothing
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
Syntax: fso.CopyFile (source, destination[, overwrite])
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")
'File to be copied Sourcefile="C:\copy.txt"'Dest folder where the file has to
be copied
Destination="D:\final1\"
'If the destination doesnot exist then create the destination folder
If fso.FolderExists(Destination) = false Then
fso.CreateFolder (Destination)
End If
'Copy the file
fso.CopyFile Sourcefile,Destination,True
Set fso=nothing
Method: DeleteFile
Description: Deletes a specified file
Syntax: fso.DeleteFile (filename[, force])
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
Description: Creates a new folder in the specified location
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")
'Folder to be created Foldername="D:\Folder_create"
'If the folder doenot exst then create the folder
If fso.FolderExists(Foldername) = false Then
fso.CreateFolder (Foldername)
End If
Set fso=nothing
Method: CopyFolder
Description: Copies a folder to a new location
Syntax: fso.CopyFolder (source, destination[, overwrite])
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
If fso.FolderExists(DestinationPath) = false Then
fso.CreateFolder (DestinationPath)
End If
fso.CopyFolder SourcePath,DestinationPath,True
Set fso=nothing
Method: MoveFolder
Description: Moves one or more folders from one location to another.
Syntax: fso.MoveFolder (source, destination)
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")
'Folder to be created SourcePath="D:\Folder_move"
DestinationPath="D:\Destination\"
'If the folder doesnot exst then create the folder
If fso.FolderExists(DestinationPath) = false Then
fso.CreateFolder (DestinationPath)
End If
fso.MoveFolder SourcePath,DestinationPath
Set fso=nothing
Method: DeleteFolder
Description: Deletes the specified folder and its contents
Syntax: fso.DeleteFolder (folderspec[, force])
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
Description: Determines whether or not a specified drive exists
Syntax: fso.DriveExists (drivespec)
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
Description: Determines whether or not a specified file exists
Syntax: fso.FileExists (filespec)
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
Description: Determines whether or not a specified folder exists
Syntax: fso.FolderExists (folderspec)
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
Syntax: objDrv = fso.GetDrive(drivespec)
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")
'Drive for getting details Sourcefile="C:\" Set
get_drv=fso.GetDrive(Sourcefile)
'Some of the following details can be retrieved from a drive
msgbox get_drv.AvailableSpace
Msgbox get_drv.DriveLetter
msgbox get_drv.DriveType
msgbox get_drv.FileSystem
msgbox get_drv.FreeSpace
msgbox get_drv.Path
Msgbox get_drv.RootFolder
Msgbox get_drv.SerialNumber
Msgbox get_drv.TotalSize
Set fso=nothing
Method: GetFolder
Description: Returns a Folder object corresponding to the folder in a specified path
Syntax: objFolder = fso.GetFolder(folderSpec)
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")
'Folder for getting details
Sourcefolder="D:\QTP"
Set get_folder=fso.GetFolder(Sourcefolder)
'get the subfolders count in a folder
Set get_subfolder=get_folder.SubFolders
For each sfile in get_subfolder
'Get the name of each folder
Msgbox sfile.name
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.
Syntax: objFile = fso.GetFile(fileSpec)
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")
'File for getting details Sourcefile="D:\qtptest.txt"
Set get_file=fso.GetFile(Sourcefile)
'Some of the following details can be retrieved from a file
msgbox get_file.DateCreated
msgbox get_file.DateLastAccessed
msgbox get_file.DateLastModified
msgbox get_file.ParentFolder
msgbox get_file.Path
Set fso=nothing
Text Stream Object Methods:
Method: Close
Description: Closes an open TextStream file
Syntax: objTso.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)
'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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Method: Read
Description: Reads a specified number of characters from a TextStream file and returns the
resulting string.
Syntax: strChars = objTso.Read(numCharacters)
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.
Syntax: strChars = objTso.ReadAll
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
Msgbox qfile.ReadAll 'Output --> Displays the entire file.
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Method: ReadLine
Description: Reads an entire line (up to, but not including, the newline character) from a
TextStream file and returns the resulting string.
Syntax: strChars = objTso.ReadLine
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
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Method: Write:
Description: Writes a specified string to a TextStream file.
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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Method: WriteLine
Description: Writes a specified string and newline character to a TextStream file.
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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Method: WriteBlankLines
Description: Writes a specified number of newline characters to a TextStream file.
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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Property: AtEndOfLine
Description: Indicates whether the file pointer is positioned immediately before the end-of-line
marker in a TextStream file.
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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Property: AtEndOfStream
Description: Indicates whether the file pointer is positioned at the end of a TextStream file.
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
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing