Pages

Understand File System Object UFT/QTP/VBA

The File System Object (FSO) object model provides an easy object-based model for working with folders and files. Using FSO object, it 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.
File System Object UFT, QTP, VBA

In UFT/QTP FSO Object significantly used perform the following operations using simple VBScript (VBA)
  • Read and Write/Update a Plain Text file (notepad)
  • Creating a directory
  • Copy a directory
  • Move a directory
  • Move a file
  • 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(FSO) - Reading 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

FSO Getting 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

FSO - Appending 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

FSO - 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

FSO - 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


FSO - 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

FSO Check Whether a File Exists


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

FSO Check Whether a Folder Exists


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


Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder ("E:\NewDir")
Set objFso = Nothing

In ideal situation first, you should check whether the folder exists. If it does not exist then you can create it.

No comments:

Post a Comment

Featured Post

How To Get Tooltip Text In UFT / QTP

While doing automation testing you may run over a circumstance wherein you need to validate the tooltip of an object in the application un...