Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Monday, 20 July 2015

Files and Folder in Vb-Scripts

FSO is a UFT object model, through which UFT/ VB Scripting allows users to process / manipulate system drives, folders and files

What can FSO do: FSO deals with files (text files) / Folders & Drives (system folders/ drives)

FSO is useful forTo create files / add data / remove data / copy files / delete files / save files and so on.

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)
'Close the files
qfile1.Close
'Release the allocated objects
Set qfile1=nothing

'Output --> New File "qtptest.txt " is created

Example-2 :  

'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
Set qfile2=fso.CreateTextFile("C:\qtptest.txt",False,False)
Set fso=nothing

'Output --> Error message "File already exists" is displayed

Methods:

CopyFile: 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


DeleteFile: 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


CreateFolder: 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

CopyFolder: 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

MoveFolder: 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


DeleteFolder: 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

DriveExists:  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


FileExists: 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

FolderExists:  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

GetDrive:  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

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


GetFolder:  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


GetFile: 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