File System Object(FSO) in UFT or QTP or VBScript

0
454
Spread the love
  • 25
  • 34
  • 12
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    106
    Shares

Introduction to FSO in VBScript or UFT

File system Object enables us to work with files like read, write, find, search a file and folder etc. FSO is an inbuilt object available in VBScript and VBScript driven UFT.FileSystemObject library provides various objects which allow the coders to interact with Windows File System.

FileSystemObject is a library under which we have several Objects or collections to work with files. FileSystemObject works only on the windows environment. FSO can work with WSH seamlessly.

Elements available in FSO Object Model

Object/Collection Details
FileSystemObject This is the root or main object of the class.This is the entry point to enter into the FSO.
We need to create object of FSO first in order to access several other methods and properties.It provides the below property

  • Drives

It provides several other functions like

  • CreateFolder()
  • FileExists(),
  • DrivesExists()
  • CopyFiles()
  • BuildPath()
  • CopyFolder()
  • CreateTextFile()
  • DeleteFile()
  • DeleteFolder()
  • FolderExists()
  • GetAbsolutePathName()
  • GetBaseName()
  • GetDrive()
  • GetDriveName()
  • GetExtensionName()
  • GetFile()
  • GetFileName()
  • GetFolder()
  • OpenTextFile()
  • MoveFolder()
  • MoveFile()
  • GetTempName()
  • GetParentFolderName()
  • GetSpecialFolder()
Drive This object provides an access to underlying disk properties along with a presentation of a logical or physical drive available in the computer where
we are running the script.It provides the below properties

  • AvailableSpace
  • LastAccessed
  • RootFolder
  • DriveLetter
  • DriveType
  • Path
  • IsReady
  • FreeSpace
  • FileSystem
  • SerialNumber
  • ShareName
  • TotalSize
  • VolumeName

It does not have any methods.

Drives -Drive Collection It is a child of FileSystemObject denotes a collection of drives(including media drives without
removable media).It can hold information about one or more drives.It provides two properties

  • Count
  • Item

It does not have any methods.

File It represents a file available in the folder or a drive.We can use either FileSystemObject.GetFile() method or
Folder or Files collection to obtain a File. It provides the below properties

  • ParentFolder
  • DateCreated
  • Attributes
  • DateLastModified
  • DateLastAccessed
  • Drive
  • Name
  • Path
  • ShortName
  • ShortPath
  • Size
  • Type

It also provides the below methods

  • Copy
  • Delete
  • Move
  • OpenAsTextStream
Files-File Collection It is a child of Folder object and denotes a collection of Files. It provides two properties

  • Count
  • Item.

We can use Folder.Files in order to get a File

It does not provide any methods.

Folder It represents the folder on a drive.We can get reference to a folder by using Drive.RootFolder or CreateFolder() ,
GetFolder() , and GetSpecialFolder() methods. It provides the below properties

  • Attribute
  • DateCreated
  • DateLastModified
  • DateLastAccessed
  • Drive
  • Name
  • Path
  • ShortName
  • ShortPath
  • Size
  • Type
  • Files
  • IsRootFolder
  • ParentFolder
  • SubFolders

It provides methods like-

  • Copy
  • Delete
  • Move
  • OpenAsTextStream
Folders- Collection of Folders It represents the collection of folder. We can get the same using Folder.SubFolders property.

This collection provides two properties

  • Count
  • Item

and a method

  • Add()

Add() method adds a subfolder to the collection.

TextStream This object represents the stream of text read from a file or written to a file or going to or coming from windows standard I/O.
This object has a line pointer and a character pointer.The direction of reading or writing is from top to bottom,Character by
Character or line by line.We get this object when we use File.OpenAsTestStream() or Folder.CreateTextFile() or OpenTextFile() or getStandardStream().  It provides properties or methods like Read , Write , ReadLine , WriteLine ,AtEndOfLine.
AtEndOfLine .

FileSystemObject Object

This is the foundation object to access the files and drives in Microsoft environment. The Properties and methods of FileSystemObject is given as:

FileSystemObject Properties
Properties Comment
Drives Returns the collection of drives including network drives available in the file system.
FileSystemObject Methods
Methods Comment
CopyFile (sourceFilePath,
destinationFilePath, overwrite)
It copies a specified file to the provided destination folder.Overwrite is a boolean argument
indicating overwrite is permitted if set as Yes.It will throw an error if there is a same file name
already exists and overwrite flag is false.
BuildPath(path,name) This method adds the folder to the path.It can automatically add separator.It can not validate the correctness of the path.
CopyFolder (sourcePath,
destinationPath, overwrite)
It copies a specified folder to the provided destination folder.Overwrite is a boolean argument
indicating overwrite is permitted if set as Yes.It will throw an error if there is a same folder name
already exists and overwrite flag is false.
CreateFolder (folderName) It creates a folder with the name specified and return the object as a reference.If the folder name already exist and we are
trying to create one more with the same name,It will throw an error
CreateTextFile (filename,
overwrite, unicode)
This method creates a text file in the specified folder with the specified fileName.
Overwrite is a boolean argument
indicating overwrite is permitted if set as Yes. The default overwrite parameter is false.
The last parameter is the indication of what format the text will be stored. Default for Unicode is false.If we set it as true the whole text will be placed in Unicode.
DeleteFile (fileName,
force)
Deletes a file or files .The force parameter may be set as true.However force is an optional parameter.
By default it is false.The file/files will be deleted even if the read only attribute is set by the user for any file.
DeleteFolder (folderName,
force)
Deletes a folder or folders.The force parameter may be set as true.However force is an optional parameter.
By default it is false.The folder/folders will be deleted even if the read only attribute is set by the user for any file.
DriveExists (driveName) This method returns true if the drive letter exists otherwise it returns false.
FileExists (fileName) This method returns true if the file exists otherwise it returns false.
FolderExists (folderName) This method returns true if the folder exists otherwise it returns false.
GetAbsolutePathName
(path)
This method takes the path and returns the absolute path of the folder. Wildcard is accepted while calculating the path.
GetBaseName (filespec) This method just returns the name of the file specified in the specification by removing the path information.
GetDrive (drivespec) This method just returns the name of the drive as an object specified in the specification by removing the path information.
GetDriveName (drivespec) This method just returns the name of the drive specified in the specification by removing the path information.
GetExtensionName
(filespec)
This method returns the extension name for the file specified in the specification.
GetFile (filespec) This method return the file object specified in the file specification. File Specification may contain absolute or relative path.
GetFileName (pathspec) This method returns just the name of the file specified in the path specification.
GetFileVersion (filespec) This method returns the version of the file specified in the path specification.
GetFolder (folderspec) This method return the folder object specified in the file specification. Folder Specification may contain absolute or relative path.
GetParentFolderName
(pathspec)
This method returns just the name of the parent folder specified in the path specification.
GetSpecialFolder
(folderspec)
This method returns the Folder object with the below mentioned special permissions:
WindowsFolder (0)
SystemFolder (1)
TemporaryFolder (2)
GetTempName() This method generates a file randomly and returns the file name to perform some activities.
MoveFile (source,
destination)
Moves the file to the destination folder from source folder specified. If the destination folder already has a file with
the same name, WSH throws an error.
MoveFolder (source,
destination)
Moves the folder to the destination folder from source folder specified. If the destination folder already has a folder with
the same name, WSH throws an error.
OpenTextFile (filename,
iomode, create, format)
This method opens a file and return text as TextStreamObject. This can be used to read,write and
append to and from the file.
IOModes are:
ForReading (1)
ForWriting (2)
ForAppending (8)FileFormats are:
TristateFalse (0) to open it as ASCII,
TristateTrue (-1) to open it as Unicode
TristateUseDefault (-2) to open it using the system default format.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 25
  • 34
  • 12
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    106
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here