What Is FSO?: We Would Use The Same Example For The Rest of This Post

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Many a times you may need to interact with text files using QTP.

Interaction can be(but not limited to) in the form of reading input from a file, writing output to a file. We use FSO object to do this. What is FSO? FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll) The FSO Object Model has a rich set of properties, methods and events to process folders and files. How to create a file? We first create a FSO object using CreateObject and then create a text file using CreateTextFile. For Example: Suppose you want to create a file called "test.txt" located in C: Dim fso, file, file_location file_location = "C:\file_location" Set fso = CreateObject(Scripting.FileSystemObject) Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists else false We would use the same example for the rest of this post. How to open a file? Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument can be ForReading, ForWriting, ForAppending //3rd argument is "True" if new file has to be created if the specified file doesnt exist else false, blank signify false. How to read content from a file? Use ReadLine() method For example: Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a file Do while file.AtEndofStream <> True data = file.ReadLine() msgbox data Loop How to write content to a file? You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesnt insert a new line character. For example:

Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a file file.Write("This is a place to get all your qtp") file.Write("questions and answers solved.") //Output will be: This is a place to get all your qtp questions and answers solved. while file.WriteLine("This is a place to get all your qtp") file.Write("questions and answers solved.") //Output will be: This is a place to get all your qtp questions and answers solved. How to delete content? Use DeleteFile() method to delete a file from a particular location Foe Example: file_location = "C:\file_location" Set fso = CreateObject(Scripting.FileSystemObject) fso.DeleteFile(file_location)

To set a variable to the CreateFolder function, use this syntax: Sub CreateFolder Dim fso, fldr Set fso = CreateObject("Scripting.FileSystemObject") Set fldr = fso.CreateFolder("C:\MyTest") Response.Write "Created folder: " & fldr.Name End Sub Once you have a handle to an object, you can access its properties. For example, to get the name of a particular folder, first create an instance of the object, then get a handle to it with the appropriate method (in this case, the GetFolder method, since the folder already exists). Use this code to get a handle to the GetFolder method: Set fldr = fso.GetFolder("c:\") Now that you have a handle to a Folder object, you can check its Name property. Response.Write "Folder name is: " & fldr.Name

You might also like