Enable and Disable Overwriting
'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 "File already exists" is displayed
Set qfile2=fso.CreateTextFile("C:\qtptest.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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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 (file://computer2/share1 |
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:
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 object’s methods and properties.
Syntax: objFile = fso.GetFile(fileSpec)
Arguments:
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 object’s 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:
⇒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:
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:
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:
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:
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:
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:
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:
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:
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
No comments:
Post a Comment