Use of FileSystemObject

File System Object is an important concept. It is basically used for interacting with Computer’s File System. It perform operations like Creating folder,deleting folder,Creating Text file,reading ,writing etc.

Let’s see use of FileSystemObject in details.

Methods: 

BuildPath

Syntax:  object.BuildPath(path, name)
This method will append a name onto an existing path.
e.g. Set FSO = createObject(“Scripting.FileSystemObject”)
msgbox FSO.BuildPath(“C:\Framework”,”Mypath”)
Output : C:\Framework\Mypath


CopyFile

Syntax: object.CopyFile source, destination [,overwrite]
This method will copy one or more files from one location (the source) to another location (destination).
Set FSO = createObject(“Scripting.FileSystemObject”)
FSO.CopyFile “C:\Sample.txt”,”D:\”
Output: Sample.txt file will be copied From C driver to D drive.


CopyFolder

Syntax: object.CopyFolder source, destination [, overwrite]
Copies one or more folders with its all content including files and subfolders, from one location to another.
Set FSO = createObject(“Scripting.FileSystemObject”)
FSO.CopyFolder “D:\Myfolder”,”C:\”
Output: Folder and its content will be copied From D driver to C drive.


CreateFolder

Syntax: object.CreateFolder (foldername)
This method will create a folder with the specified folder name.
FSO.CreateFolder “D:\Myfolder1”
Output: It will create the Folder Myfolder1 in D driver.


CreateTextFile

Syntax: object.CreateTextFile filename [,overwrite[, unicode]]
Creates a text file and returns a TextStreamObject that can then be used to write to and read from the file.
FSO.CreateTextFile “C:\Sample.txt”
Output: It will create the text file with a name Sample.


 

 

DeleteFile

Syntax: object.DeleteFile file [,force]
This method deletes a specified file or files.
FSO. DeleteFile “C:\Sample.txt”
Output: It will delete the Sample.txt file.


DeleteFolder

Syntax: object.DeleteFolder folder [,force]
This method deletes a specified folder, including all files and subfolders.
FSO. DeleteFolder “C:\SampleFolder “
Output: It will delete the SampleFolder.


DriveExists

Syntax: object.DriveExists(drive)
This method checks if a specified drive exists. It returns True, if the drive exists and False, if it doesn’t.
msgbox FSO.DriveExists(“D”)


FileExists

Syntax: object.FileExists(file)
This method checks whether a specified file exists. Returns True, if the file exist otherwise false.
FSO.FileExists(“C:\Sample.txt”)


FolderExists

Syntax: object.FolderExists(folder)
It verifies, if a specified folder exists. Returns True, if the folder does exist and False, if it doesn’t.
msgbox FSO.FolderExists(“D:\SampleFolder”)


GetAbsolutePathName

Syntax: object.GetAbsolutePathName(path)
This method gets the complete path from the root of the drive for the specified path string.
msgbox FSO.GetAbsolutePathName(“\SampleFolder”)
Output: C:\SampleFoldre  as this folder exist in my C drive.


GetBaseName

Syntax: object.GetBaseName(path)
This method gets the base name of the file or folder in a specified path.
FSO.GetBaseName(“D:\SampelFolder\Sample.txt”)
Output: Sample as base name


GetDrive

Syntax: object.GetDrive(drivename)
This method will return a Drive object corresponding to the drive in a supplied path and we can perform operation on that drive like available space,Total size etc.
Set Ddriver= FSO.GetDrive(“D:\SampelFolder\Sample.txt”)
Ddriver.FreeSpace
Output: Free space available in D drive


GetDriveName

Syntax: object.GetDriveName(path)
This method gets a string containing the name of the drive in a supplied path.
msgbox FSO.GetDriveName(“D:\SampelFolder\Sample.txt”)
Output: D:


GetExtensionName

Syntax: object.GetExtensionName(path)
This methos will return a string containing the extension name of the last component in a supplied path.
msgbox FSO.GetExtensionName(“D:\SampelFolder\Sample.txt”)
Output: txt


GetFile

Syntax: object.GetFile (filename)
This method will return the File object for the specified file name and you can perform operations like Move,DateCreated,size etc.
Set FSO = createObject(“Scripting.FileSystemObject”)
Set oFile =  FSO.GetFile(“D:\SampelFolder\Sample.txt”)
msgbox oFile.DateCreated
Output: Date and time when the file was created


GetFileName

Syntax: object.GetFileName(path)
This method will return the name of the last file or folder of the supplied path.
msgbox FSO.GetFileName(“D:\SampelFolder\Sample.txt”)
Output : Sample.txt


GetFileVersion

Syntax: object.GetFileVersion(path)
This method will return the version of the file in the specified path.
msgbox FSO.GetFileVersion(“D:\SampelFolder\Sample.txt”)
Output: Version number if any


GetFolder

Syntax: object.GetFolder (foldername)
This method will return a Folder access and we can perform the operations like move folder ,copy folder, size etc.
Set oFolder= FSO.GetFolder(“D:\SampelFolder”)
oFolder.Copy(“C:\”)
Output: It will copy the SampelFolder in to C Drive.


GetParentFolderName

Syntax: object.GetParentFolderName(path)
Returns a string containing the name of the parent folder of the last file or folder in a specified path.
msgbox FSO.GetParentFolderName(“D:\SampelFolder\Sample”)
Output: D:\SampelFolder\


GetSpecialFolder

Syntax: object.GetSpecialFolder (folderid)
Returns the path to access of the special folders – \WindowsFolder(0), \SystemFolderor(1) \TemporaryFolder(2).
Set TFolder= FSO.GetSpecialFolder(TemporaryFolder)


GetTempName

Syntax: object.GetTempName
This method generates a random filename for a temporary file or folder name that is useful for performing operations that require a temporary file or folder.
Set FSO = createObject(“Scripting.FileSystemObject”)
msgbox FSO.GetTempName
Output: It will generate some temporary name


MoveFile

Syntax: object.MoveFile source, destination
Moves one or more files from one location to another.
FSO.MoveFile(“C:\Sample.txt”,”D:”)
Output: It will move Sample.txt from C to D drive


MoveFolder

Syntax: object.MoveFolder source, destination
Moves one or more folders from one location to another.
FSO.MoveFolder(“C:\Sample”,”D:”)
Output: It will move Sample named folder from C to D drive


OpenTextFile

Syntax: object.OpenTextFile (filename [, iomode[, create[, format]]])
Opens the file specified in the filename parameter and returns an instance of the TextStreamObject for that file and we can perform read and write operation on that file.
ForReading = 1, ForWriting = 2, ForAppending = 8

Set MyFile =  FSO.OpenTextFile(“C:\Sample.txt”, ForWriting)
MyFile.Write “Hello World”


 

In next session, we will see how to create the file,reading and writing operations as well using FSO.

Print Friendly, PDF & Email
Create ,Read and write a text file
Integrating with ALM
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *