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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 9 |
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.
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 |
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
1 2 3 4 5 6 7 |
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
1 2 3 4 5 6 7 8 |
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
1 2 3 4 |
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
1 2 3 4 |
Set objFso = CreateObject("Scripting.FileSystemObject") sFolderPath = "D:\TestFolder\TempDir" objFso.DeleteFolder sFolderPath Set objFso = Nothing |
Recommended Posts
- Read, Write and Update Excel File In UFT
- Web Table Methods in UFT With Example
- How To Use Dictionary Object in UFT With Examples
- All You Need to Know About Object Identification in UFT
- Reading and Updating MS Access Database Using ADODB in UFT
- Read Excel Using ADODB Connection in UFT | VBA
- DataTable in UFT One | Example of Datatable Methods
- Read and Update XML File in UFT | VBA