The File System Object (FSO) object model provides an easy object-based model for working with folders and files. The FSO object exposes a comprehensive set of properties and methods to perform various file system operations such as reading, writing, creating, moving, deleting, and providing information about folders and files.
In UFT/QTP FSO Object significantly used to perform the following operations using simple VBScript (VBA)
Read and Write/Update a Plain Text file (notepad)
Creating a directory
Check whether a directory exists in the specified path
Create a file
Check whether a file exists in the required path
Delete a File
Delete a Directory
File System Object in VBScript – Read a Text File
Const ForReading = 1
Dim objFso , objFile , FileName , TextLine
FileName = "E:\Test.txt" 'Provide your file path
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
Set objFile = objFso . OpenTextFile ( FileName , ForReading )
'Read from the file
Do While objFile . AtEndOfStream < > True
TextLine = objFile . ReadLine
MsgBox TextLine
'Do your stuff
Loop
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
File System Object in UFT – Get Entire Text of a File in a Variable
Const ForReading = 1
Dim objFso , objFile , FileName , strText
FileName = "E:\Test.txt" 'Provide your file path
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
Set objFile = objFso . OpenTextFile ( FileName , ForReading )
strText = objFile . ReadAll
MsgBox strText
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
File System Object – Append Text in an Existing File
Const ForAppending = 8
Dim objFso , objFile , FileName , TextLine
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
FileName = "E:\Test.txt" 'Provide your file path
Set objFile = objFso . OpenTextFile ( FileName , ForAppending )
objFile . WriteLine "This is just a new test line"
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
File System Object – Create or Open an Existing File and Append Text
The below code will check whether the provided file exists. If the file exists it will open the file and write the required text. If the file does not exist, it will create the required file and write the required text in it.
Const ForAppending = 8
Dim objFso , objFile , FileName , TextLine
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
FileName = "E:\NewFile.txt" 'Provide your file path
Set objFile = objFso . CreateTextFile ( FileName , ForAppending )
objFile . WriteLine "This a test line"
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
File System Object – Open an Existing File and Overwrite it
Const ForWriting = 2
Dim objFso , objFile , FileName , TextLine
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
FileName = "E:\Test.txt" 'Provide your file path
Set objFile = objFso . OpenTextFile ( FileName , ForWriting )
objFile . WriteLine "My Sample Text"
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
File System Object – Create a Text file and Write Text in it
Const ForWriting = 2
Dim objFso , objFile , FileName , TextLine
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
FileName = "E:\Test3.txt"
Set objFile = objFso . CreateTextFile ( FileName , ForWriting )
objFile . WriteLine "My Sample Text.Hello World"
objFile . Close
Set objFile = Nothing
Set objFso = Nothing
Check Whether a File Exists Using File System Object
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
If ( objFso . FileExists ( "E:\Test1.txt" ) ) Then
MsgBox "File exists"
Else
MsgBox "File does not exist"
End If
Set objFso = Nothing
Check Whether a Folder Exists Using File System Object
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
If ( objFso . FolderExists ( "E:\MyDir" ) ) Then
MsgBox "Folder exists"
Else
MsgBox "Folder does not exist"
End If
Set objFso = Nothing
File System Object – Create a Folder if it does not Exist
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
sFolderPath = "D:\TestFolder"
If ( objFso . FolderExists ( sFolderPath ) ) Then
MsgBox "Folder exists"
Else
objFso . CreateFolder ( sFolderPath )
End If
Set objFso = Nothing
Delete a File Using File Using System Object VBScript in UFT
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
sFilePath = "D:\TestFolder\abc.txt"
objFso . DeleteFile sFilePath
Set objFso = Nothing
Delete a Folder Using File System Object VBScript in UFT
Set objFso = CreateObject ( "Scripting.FileSystemObject" )
sFolderPath = "D:\TestFolder\TempDir"
objFso . DeleteFolder sFolderPath
Set objFso = Nothing
Recommended Posts