QTP – FSO (File System Object)
FSO: File System Object
What
is FSO:
FSO is a QTP object model,
through which QTP / VB Scripting allows users to process / manipulate
system drives, folders and files
What
can FSO do:
FSO deals with files (text files) / Folders & Drives
(system folders/drives)
FSO
is useful for:
To create files / add data / remove data / copy
files / delete files / save files and so on.
Creating Files
To create an empty text file use
the CreateTextFile method.
The following example demonstrates how to create a
text file using theCreateTextFile method.
Dim fso, f1
Set fso =
CreateObject("Scripting.FileSystemObject")
Set f1 =
fso.CreateTextFile("C:\testfile.txt", True)
' True to overwrite if there is already a file
Opening Files
To open existing files use “OpenTextFile” method
of the FileSystemObjectobject with the ForWriting flag
(true/false).
Dim fso, file
Const ForWriting = 2
' Here ForWriting = 2 is a constant for
writing data to a file
Set fso = CreateObject("Scripting.
FileSystemObject")
Set file =
fso.OpenTextFile("C:\test.txt", ForWriting, True)
' True indicates whether a new file can be created
if the specified filename doesn't exist
' Another method OpenAsTextStream to open a
text file
Dim fso, file, file1
Const ForWriting = 2
' Here ForWriting = 2 is a constant for
writing data to a file
Set fso =
CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
' To get a file
Set file = fso.GetFile("c:\test1.txt")
' To Open a file
Set file1 = file.OpenAsTextStream(ForWriting, True)
' True indicates whether a new file can be created
if the specified filename doesn't exist
Writing data to the File
Once the text file is created, add data to the file
using the following three steps:
·
Open the text file.
·
Write the data.
·
Close the file.
To open an existing file, use either
the OpenTextFile method of the FileSystemObject object or
the OpenAsTextStreammethod of the File object.
To write data to the open text file, use
the below methods
Write
WriteLine
WriteBlankLines
The following table describes which method to use
for various tasks
Method
|
Task
|
Write
|
Write data to an open
text file without a trailing newline character.
|
WriteLine
|
Write data to an open
text file with a trailing newline character.
|
WriteBlankLines
|
Write one or more blank
lines to an open text file.
|
To close an open file, use
the Close method of the TextStream object.
Consider the below example:
Function CreateFile()
Dim fso, file
Set fso =
CreateObject("Scripting.FileSystemObject")
Set file =
fso.CreateTextFile("c:\testfile.txt", True)
' Write a line with a newline
character.
file.WriteLine(" Hey, Write my
Name here :::G A Reddy :):):) .")
' Write three newline characters to
the file.
file.WriteBlankLines(3)
' Write a line.
file.Write ("This is a test text
: writing into a file.")
file.Close
End Function
Reading data from Files
To read data from a text file, use the
Read
ReadLine
ReadAll
These methods are from
the TextStream object.
The following table describes which method to use
for various tasks.
Method
|
Task
|
Read
|
Read a specified number
of characters from a file.
|
ReadLine
|
Read an entire line (up
to, but not including, the newline character).
|
ReadAll
|
Read the entire contents
of a text file.
|
If you use
the Read or ReadLine method and want to skip to a
particular portion of data, use the Skip or SkipLine method.
The resulting text of the read methods is stored in a string which can be
displayed in a control, parsed by string functions (such
as Left, Right, and Mid), concatenated, and so on.
The following example demonstrates how to open a
file, write data to it and then read from it:
Function ReadFiles()
Dim fso, file, file1, str
Const ForReading = 1
Set fso =
CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\testfile.txt",
True)
' Write a line.
Print "Started Writing data to a
file"
file.WriteLine "Hey, How are you my
Friend"
file.WriteBlankLines(1)
file.Close
' Read the contents of the file
Print
"Started Reading data from file "
Set file1 =
fso.OpenTextFile("c:\testfile.txt", ForReading)
str= file1.ReadLine
Print "File contents =
'" & str & "'"
file1.Close
End Function
Moving, Copying, and Deleting Files
The FSO object model has two methods each for
moving, copying, and deleting files, as described in the following table.
Method
|
Task
|
File.Move or
FileSystemObject.MoveFile
|
To Move a file
|
File.Copy or
FileSystemObject.CopyFile
|
To Copy a file
|
File.Delete or
FileSystemObject.DeleteFile
|
To Delete a file
|
The following example creates a text file in the
root directory of drive C, writes some information to it, moves it to a
directory called \temp1, makes a copy of it in a directory called \temp2, then
deletes the copies from both directories.
To run the following example, create directories
named \temp1 and \temp2 in the root directory of drive C:
Function ManipFiles()
Dim fso, file1, file2, str
Set fso =
CreateObject("Scripting.FileSystemObject")
Set file1 =
fso.CreateTextFile("c:\testfile.txt", True)
Print "Writing file "
' Write a line.
file1.Write ("My Friend, I am
teaching about Copying and moving a file ")
' Close the file to writing.
file1.Close
Print "Moving file to c:\temp1"
' Get a handle to the file in root of
C:\.
Set file2 =
fso.GetFile("c:\testfile.txt")
' Move the file to \temp1 directory.
file2.Move
("c:\temp1\testfile.txt")
Print "Copying file to c:\temp2"
' Copy the file to \temp2.
file2.Copy
("c:\temp2\testfile.txt")
Print "Deleting files now"
' Get handles to files' current
location.
Set file2 =
fso.GetFile("c:\temp1\testfile.txt")
Set file3 =
fso.GetFile("c:\temp2\testfile.txt")
' Delete the files.
file2.Delete
file3.Delete
Print "Files
operation copying and moving and deleting is complete :) "
End Function
QTP and FSO Methods:
Adds a key and item pair to a Dictionary object.
Adds a new folder to a Folders collection.
Appends a name to an existing path.
Closes an open TextStream file.
Copies a specified file or folder from one location
to another.
Copies one or more files from one location to
another.
Recursively copies a folder from one location to
another.
Creates a folder.
Creates a specified file name and returns a TextStream object
that can be used to read from or write to the file.
Deletes a specified file or folder.
Deletes a specified file.
Deletes a specified folder and its contents.
Returns true if the specified drive exists; false
if it does not.
Returns true if a specified key exists in the Dictionary object,
false if it does not.
Returns true if a specified file exists; false if
it does not.
Returns true if a specified folder exists; false if
it does not.
Returns a complete and unambiguous path from a
provided path specification.
Returns a string containing the base name of the
last component, less any file extension, in a path.
Returns a Drive object corresponding to
the drive in a specified path.
Returns a string containing the name of the drive
for a specified path.
Returns a string containing the extension name for
the last component in a path.
Returns a File object corresponding to
the file in a specified path.
Returns the last component of specified path that
is not part of the drive specification.
Returns the version number of a specified file.
Returns a Folder object corresponding to
the folder in a specified path.
Returns a string containing the name of the parent
folder of the last component in a specified path.
Returns the special folder object specified.
Returns a TextStream object corresponding
to the standard input, output, or error stream.
Returns a randomly generated temporary file or
folder name that is useful for performing operations that require a temporary
file or folder.
Returns an array containing all the items in a Dictionary object.
Returns an array containing all existing keys in a Dictionary object.
Moves a specified file or folder from one location
to another.
Moves one or more files from one location to
another.
Moves one or more folders from one location to
another.
Opens a specified file and returns a TextStream object
that can be used to read from, write to, or append to the file.
Opens a specified file and returns a TextStream object
that can be used to read from, write to, or append to the file.
Reads a specified number of characters from a TextStream file
and returns the resulting string.
Reads an entire TextStream file and
returns the resulting string.
Reads an entire line (up to, but not including, the
newline character) from aTextStream file and returns the resulting string.
Removes a key, item pair from a Dictionary object.
Removes all key, item pairs from a Dictionary object.
Skips a specified number of characters when reading
a TextStream file.
Skips the next line when reading a TextStream file.
Writes a specified string to a TextStream file.
Writes a specified number of newline characters to
a TextStream file.
Writes a specified string and newline character to
a TextStream file.
Related Sections
List of elements that make up Scripting Run-Time
Reference.
Note: All these FSO methods and functions are
explained clearly (example by example) in trainings or in later editions
0 comments:
Post a Comment