Usage of File system object (FSO) in Vbscript. QTP / UFT

In this post we will see File system object usage in vb scripting with below mentioned operations

  •  File system object introduction
  •  Create  text file
  •  Access as existing drive, folder and file
  •  Verify existence of file
  •  Verify existence  of folder
  •  Create new folder
  •  Write text in file
  •  Read text from file
  •  Copy file paste file
  •  Move  file
  •  Delete file
  •  Difference  between readAll and readline method

      Fetch drive, folder and file information

File system object introduction -: File system object is very useful while perform folder, file and drive operations likely add, move, change, create, delete and fetch object information.
Scripting allow you to perform above operations on windows environment.
FSO object provides access to below mentioned collections object.

Drive -: this object provides object and methods to gather information about drive such as how much space is available, how much space consumed, and drive could be physically attached or logically connect to the system.

Drives -: this collection provides information such as list of drives connected to the system it could be physically or logically connected.

File -: this object provides methods to create, delete, copy and move files under given location.

Files-: this collection provides list of all file contained within folder.

Folder -: this object provides methods to create, delete, copy and move folder under given location.

Folders -: this collection provides list of all folders contained within folder.

Textstream -: Another important object which provides read, write operation on text files.
Usage of above object and collations are mentioned below
In above intro part we seen fso object supported operations and methods, let’s have a look how it works.

How to create FSO object
Using file system object we can interact with drive, folder and files, for interaction with object provides the way of access with in object and we can read, write or print the useful information.
First initiate fso object to perform interaction with objects.

Dim ObjectFSO
Set ObjectFSO = CreateObject(“Scripting.FileSystemObject”)


Above example of fso object creation and remember this is the standard way of fso object creation.
Create Text file -: File system object provides way to create new text file with various extensions such as .txt, .log, .bat etc. that file can used to write text in a file.

For example -:

Dim MyTextFile
Set MyTextFile = ObjectFSO.CreateTextFile(FilePath, overwrite option, Unicode/ASCII option)
‘ Parameter details
FilePath – File location where file gets  stored
Overwritten option – True for overwrite file if exist and false for not overwrite, if file exist omit the operation.
Unicode or ASCII option – True for create file with type Unicode and false for create file for ASCII file.
Access as Existing Drive, Folder and File -: FSO object provides functionality gather information of present drives such drive type, consumed memory, available memory, total memory etc.

 Drive methods-:

‘ To get drive name
Set drive = ObjectFSO.GetDrive(ObjectFSO.GetDriveName(“c:\”))
Msgbox drive.VolumeName
‘ To get free space of drive
Msgbox FormatNumber(drive.FreeSpace/1024, 0)
Folder Methods-: Folder object create object instance and we can use it to create folder, delete folder, move folder and gather folder information.

‘ To get existing folder and new folder
Set MyRootFolder = ObjectFSO.GetFolder(“c:\Root Folder path”)
Set MySubFolders = MyRootFolder.SubFolders
MySubFolders.Add(“FolderName”)
‘ verify existence  of folder
ObjectFSO.FolderExists(folderpath)
‘ Fetch folder information – Created date
Msgbox MyRootFolder.DateCreated
‘ Fetch folder information – Last accessed date
Msgbox MyRootFolder.DateLastAccessed
‘ Fetch folder information – Last Modified date
Msgbox MyRootFolder.DateLastModified
‘ Fetch folder information – Drive information
Msgbox MyRootFolder.Drive
‘ Fetch folder information – get Parent Folder
Msgbox MyRootFolder.ParentFolder
‘ Fetch folder information – get Folder type
Msgbox MyRootFolder.Type
‘ Fetch folder information – get size of Folder
Msgbox MyRootFolder.Size
Files Method -: Textstream object helps to work with text files, we can read, write or append the contents to the text file using the text stream object
      
‘ Create text file
Set ObjFile = ObjectFSO.CreateTextFile("C:\test.txt")
‘’ Open existing file
Set ObjFile = ObjectFSO.OpenTextFile("C:\test.txt",ForAppend,True)
‘’ Parameter details

           1.    File path
           2.    File opening modes
a)   For reading – 1
b)    For writing – 2
c)    For appending - 8
           3.    Format
a)    TristateTrue = -1 – Open file as unicode format
b)    TristateFalse – 0 – Open file as ASCII
c)    TristateDefault = -2 – Open file in system default format
‘’Write data in text file
ObjFile.Write(“VB script write text example”)  --‘ Write the text in text file
ObjFile.Writeline(“VB script writeline text example”)  --‘ Write the text in text file and append new line.
‘’Read text from text file
Set ObjFile = ObjectFSO.OpenTextFile("C:\test.txt",ForReading)
’Read file content line by line
Do while ObjFile.AtEndOfStream <> True
      Msgbox ObjFile.Readline

Loop


for example you can copy below programs code and paste in notepad and save with .vbs

       1.    Assigment01_CreateTextFile.vbs

’How to Create text file
Dim ObjectFSO
Dim MyTextFile
Dim sFilePath
'Assign File location path
sFilePath = "C:\TestFile.txt"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
Set MyTextFile = ObjectFSO.CreateTextFile(sFilePath, 1, True)
'' verify file existence
if ObjectFSO.FileExists(sFilePath) then
   msgbox "File exist = " & sFilePath
   MyTextFile.close
Else
      msgbox "File Not exist "
End if
Set MyTextFile = Nothing
Set ObjectFSO = Nothing
       2.    Assigment02_Folder_Create.vbs

‘ Folder create
Dim ObjectFSO
Dim MyFolder
Dim sFolderPath
'Assign File location path
sFolderPath = "C:\TestFolder"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
'Set MyFolder = ObjectFSO.CreateFolder(sFolderPath)
'' verify file existence
if ObjectFSO.FolderExists(sFolderPath) then
   msgbox "Folder exist = " & sFolderPath
Else
      msgbox "Folder Not exist "
End if
Set MyFolder = Nothing
Set ObjectFSO = Nothing
      3.    Assigment03_Access_Drives.vbs

‘’Gather Drive information
Dim ObjectFSO
Dim MyFolder
Dim sFolderPath
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
Set drive = ObjectFSO.GetDrive(ObjectFSO.GetDriveName("c:\"))
Msgbox drive.VolumeName
Msgbox "Free space available on "& drive.VolumeName & " drive " & FormatNumber(drive.FreeSpace/1024, 0) & " Kbytes"
''release objects
Set drive = nothing
Set ObjectFSO = nothing
       4.     Assigment04_Write_Text_InTextFile.vbs

‘write text file
Dim ObjectFSO
Dim MyTextFile
Dim sFilePath
'Assign File location path
sFilePath = "C:\My Home\FSO Tutorial\TestFile.txt"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
Set MyTextFile = ObjectFSO.CreateTextFile(sFilePath, 2, False)
MyTextFile.Write("VB script write function ")
MyTextFile.Writeline("VB script writeline function1")
MyTextFile.Writeline("VB script writeline function2 ")
MyTextFile.Close
Set MyTextFile = Nothing
     Set ObjectFSO = Nothing
       5.    Assigment05_Read_Text_InTextFile.vbs

‘’Read text file
Dim ObjectFSO
Dim MyTextFile
Dim sFilePath
Const ForReading = 1
'Assign File location path
sFilePath = "C:\My Home\FSO Tutorial\TestFile.txt"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
msgbox ObjectFSO.OpenTextFile(sFilePath,ForReading).readall
Set ObjectFSO = Nothing
      6.    Assigment06_Copy_PasteFile.vbs

‘Copy file and paste file
Dim ObjectFSO
Dim sFilePath1,sFilePath2
'Assign File location path
sFilePath1 = "C:\My Home\FSO Tutorial\TestFile.txt"
sFilePath2 = "C:\My Home\FSO Tutorial\TestFolder\"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
ObjectFSO.CopyFile sFilePath1,sFilePath2
Msgbox "File Copied"
Set ObjectFSO = Nothing
       
       7.    Assigment07_Delete_File.vbs

‘’Delete existing file from location
Dim ObjectFSO
Dim sFilePath
'Assign File location path
sFilePath = "C:\My Home\FSO Tutorial\TestFile.txt"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
ObjectFSO.DeleteFile(sFilePath)
Set ObjectFSO = Nothing
       8.    Assigment08_Move_File.vbs

’Move file from source to destination folder
Dim ObjectFSO
Dim sFilePath1,sFilePath2
'Assign File location path
sFilePath1 = "C:\My Home\FSO Tutorial\TestFile.txt"
sFilePath2 = "C:\My Home\FSO Tutorial\TestFolder\"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
ObjectFSO.MoveFile sFilePath1,sFilePath2
Set ObjectFSO = Nothing
Msgbox "File moved"
       9.    Assigment08_Delete_Folder.vbs

‘’Delete existing folder
Dim ObjectFSO
Dim MyFolder, sFolderPath
'Assign File location path
sFolderPath = "C:\My Home\FSO Tutorial\TestFolder"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
'' verify file existence
if ObjectFSO.FolderExists(sFolderPath) then
   msgbox "Folder exist = " & sFolderPath
Else
      msgbox "Folder Not exist "
End if
ObjectFSO.DeleteFolder(sFolderPath)
'' verify file existence
if ObjectFSO.FolderExists(sFolderPath) then
   msgbox "Folder exist = " & sFolderPath
Else
      msgbox "Folder Not exist "
End if
Set MyFolder = Nothing
Set ObjectFSO = Nothing
       10. Assigment08_Move_Folder.vbs

‘’Move folder from source to destination folder
Dim ObjectFSO
Dim MyFolder
Dim sFolderPath1,sFolderPath2,FolderMovedPath
'Assign File location path
sFolderPath1 = "C:\My Home\FSO Tutorial\Test1"
sFolderPath2 = "C:\My Home\FSO Tutorial\Test2\"
FolderMovedPath = sFolderPath2 & "\Test1"
Set ObjectFSO = CreateObject("Scripting.FileSystemObject")
ObjectFSO.MoveFolder sFolderPath1,sFolderPath2
'' verify file existence
if ObjectFSO.FolderExists(FolderMovedPath) then
   msgbox "Folder exist = " & FolderMovedPath
Else
      msgbox "Folder Not exist "
End if
Set MyFolder = Nothing
Set ObjectFSO = Nothing

Also you see above operations video tutorial
happy learning 🙂

Comments

  1. Very very useful ... Covers all FSO actions .. thankyou very much .help me to prepare for interview

    ReplyDelete
  2. Nice Blog, When I was read this blog, I learnt new things & it’s truly have well stuff related to developing technology, Thank you for sharing this blog. Need to learn software testing company, please share.

    ReplyDelete

Post a Comment

Popular posts from this blog

Excel Operation in VBScript

How to set browser language for chrome for localization testing