File Handling using FSO



What is FileSystemObject Model?



Create FSO



Creating text files using FSO



Reading contents from a File



Writing text to a File



Deleting a File



Session Wrap-Up
pgorantla.blogspot.com
File Handling using FSO



The FSO object model is contained in the Scripting type
library (Scrrun.dll), supports text file creation and
manipulation through the TextStream object.



The FSO object model gives the server-side applications
the ability to create, alter, move, and delete folders, or to
detect if particular folders exist, and if so, where.



You can also find out information about folders, such as
their names, the date they were created or last modified,
and so forth.



Create FSO Objects

FSO Objects can be created using the CreateObject Method.
Dim myfso
Set myfso = CreateObject (“Scripting.FileSystemObject”) ‘Creating a FSO and then assigning it to myfso

pgorantla.blogspot.com
File Handling using FSO




Creating Text Files using FSO.
CreateTextFile method of FSO helps to create text
files.

fsoObject.CreateTextFile (filename [, overwrite [, Unicode]]) as
TextStream.
 Parameters
 Filename – The Filename for the file to be created
 Overwrite – Boolean value indicating whether to overwrite the

file if it already exists. If the file is to be overwritten, the value
set is true, else false. Blank denotes the files are not overwritten.
 Unicode – Boolean value indicating the nature of the text file ie.,
whether the file to be created as a Unicode or an ASCII file. The
value set is True for Unicode, else False. Blank denotes ASCII
files.



Returns

 TextStream Object

pgorantla.blogspot.com
File Handling using FSO



The ReadLine() Method is used to read the contents of a text
file. Alternatively, Read() and ReadAll() methods can be used
to achieve the same objective.



Syntax for OpenTextFile() is



Object.OpenTextFile(filename[, iomode[, create[, format]]])
Parameters
 Filename – Name of the file to be opened.
 Iomode - Optional. The Mode in which the file has to be opened. The
value can toggle between these three modes: ForReading, ForWriting,
ForAppending
 Create – Optional. Boolean value that indicates whether a new file can be
created if the specified file doesn’t exist. The value is true if new file has to
be created, else false. If blank, then new file isn’t created.
 Format – Optional. Values toggle between 3 states. Whether the file has to
be opened as Unicode, opened as ASCII or use the system default
pgorantla.blogspot.com
File Handling using FSO

•

ReadLine()
– Reads all the characters till the newline character is encountered. I.e., it
reads an entire line.
– Syntax
• Object.ReadLine()

pgorantla.blogspot.com
File Handling using FSO





The Write() and WriteLine() Methods are used 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 doesn’t insert a new line character.

pgorantla.blogspot.com
File Handling using FSO



The DeleteFile() or Delete() Method can be used
to delete the file.



sample code

pgorantla.blogspot.com
File Handling using FSO

Q & A….

pgorantla.blogspot.com
File Handling using FSO







FSO objects are part of Scripting Library.
The methods and properties of FSO Objects empowers the user
to work with files and folders.
FSO Objects can be created using the CreateObject Method.
CreateTextFile method of FSO is used for creating text files
OpenTextFile method of FSP is used for opening text files. The
text files can be opened in 3 modes:








– the file is opened in Read Only Mode
ForWriting – the file is opened in ReadWrite Mode
ForAppending – the file is opened in ReadAppend Mode
ForReading

The Read() and ReadLine() Method is used for reading
contents line by line from the Text file.
The Write() and WriteLine() Method are used for writing
contents to the file.
The DeleteFile() or Delete() Method can be used to delete the
file.
pgorantla.blogspot.com

File System Object in QTP

  • 2.
    File Handling usingFSO  What is FileSystemObject Model?  Create FSO  Creating text files using FSO  Reading contents from a File  Writing text to a File  Deleting a File  Session Wrap-Up pgorantla.blogspot.com
  • 3.
    File Handling usingFSO  The FSO object model is contained in the Scripting type library (Scrrun.dll), supports text file creation and manipulation through the TextStream object.  The FSO object model gives the server-side applications the ability to create, alter, move, and delete folders, or to detect if particular folders exist, and if so, where.  You can also find out information about folders, such as their names, the date they were created or last modified, and so forth.  Create FSO Objects FSO Objects can be created using the CreateObject Method. Dim myfso Set myfso = CreateObject (“Scripting.FileSystemObject”) ‘Creating a FSO and then assigning it to myfso pgorantla.blogspot.com
  • 4.
    File Handling usingFSO   Creating Text Files using FSO. CreateTextFile method of FSO helps to create text files. fsoObject.CreateTextFile (filename [, overwrite [, Unicode]]) as TextStream.  Parameters  Filename – The Filename for the file to be created  Overwrite – Boolean value indicating whether to overwrite the file if it already exists. If the file is to be overwritten, the value set is true, else false. Blank denotes the files are not overwritten.  Unicode – Boolean value indicating the nature of the text file ie., whether the file to be created as a Unicode or an ASCII file. The value set is True for Unicode, else False. Blank denotes ASCII files.  Returns  TextStream Object pgorantla.blogspot.com
  • 5.
    File Handling usingFSO  The ReadLine() Method is used to read the contents of a text file. Alternatively, Read() and ReadAll() methods can be used to achieve the same objective.  Syntax for OpenTextFile() is   Object.OpenTextFile(filename[, iomode[, create[, format]]]) Parameters  Filename – Name of the file to be opened.  Iomode - Optional. The Mode in which the file has to be opened. The value can toggle between these three modes: ForReading, ForWriting, ForAppending  Create – Optional. Boolean value that indicates whether a new file can be created if the specified file doesn’t exist. The value is true if new file has to be created, else false. If blank, then new file isn’t created.  Format – Optional. Values toggle between 3 states. Whether the file has to be opened as Unicode, opened as ASCII or use the system default pgorantla.blogspot.com
  • 6.
    File Handling usingFSO • ReadLine() – Reads all the characters till the newline character is encountered. I.e., it reads an entire line. – Syntax • Object.ReadLine() pgorantla.blogspot.com
  • 7.
    File Handling usingFSO   The Write() and WriteLine() Methods are used 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 doesn’t insert a new line character. pgorantla.blogspot.com
  • 8.
    File Handling usingFSO  The DeleteFile() or Delete() Method can be used to delete the file.  sample code pgorantla.blogspot.com
  • 9.
    File Handling usingFSO Q & A…. pgorantla.blogspot.com
  • 10.
    File Handling usingFSO      FSO objects are part of Scripting Library. The methods and properties of FSO Objects empowers the user to work with files and folders. FSO Objects can be created using the CreateObject Method. CreateTextFile method of FSO is used for creating text files OpenTextFile method of FSP is used for opening text files. The text files can be opened in 3 modes:       – the file is opened in Read Only Mode ForWriting – the file is opened in ReadWrite Mode ForAppending – the file is opened in ReadAppend Mode ForReading The Read() and ReadLine() Method is used for reading contents line by line from the Text file. The Write() and WriteLine() Method are used for writing contents to the file. The DeleteFile() or Delete() Method can be used to delete the file. pgorantla.blogspot.com

Editor's Notes

  • #3 Briefly review the session agenda as indicated here. Currently we are in the “Prep Session Introduction” section, during which we will review basic information about this session, including how to use Centra to participate. Note: The Prep Session Introduction section should take no longer than 15 minutes.
  • #7 The difference between the three methods is: ReadLine() – Reads all the characters till the newline character is encountered. I.e., it reads an entire line Read() – Reads the specified number of characters ReadAll() – Reads all the characters in a file and returns a text stream object.
  • #8 Note: Inorder to append the new text in the file, the file has to be opened in the ForAppending mode.
  • #10 Spend a few moments answering participants’ questions about the agenda of this session or the Centra controls they will be using. Remind them to use the “Raise Hand” icon to ask a question.