http://quicktesthp.blogspot.com

QTP VBScript new series + Interview Question Bank on QTP for enrichment of Knowledge in QTP

This Site has been brought to you by HP Certified Expert of QTP.

Exciting new articles for October:

1) QTP Tip:Deselect all Radio Buttons

2) HP QTP Crypt Object

3)Adding Hyperlinks in Excel Spreadsheet

Best of Luck Friends ! ! !

Expert QTP
expert.qtp@gmail.com

All Articles are Copyright Protected. These are Free for Reading & Personal Use. Reproduction in Any Form without the Permission is Illegal & Strictly Prohibited.

Copyright © 2009 ExpertQTP

Google Search

Saturday, August 23, 2008

How to Manage your Files using QTP

Sometimes as a QTP programmer, we may need to keep track about the files inside our Operating system. Apart from tracking our files, we need to manage them dynamically like files can be created, files can be read, files can be edited and deleted. In order to accomplish this task, we require FileSystemObject. It contains methods to carry all administrative tasks related to files.

Getting access of an already created file

For this, use the below written code.

Set obj=CreateObject("Scripting.FileSystemObject")
obj.getFile("D:\testfile.txt")


In order to run this code perfectly fine, the File "testfile.txt" should be present in the D:\ location else QTP will raise an error.

As you can see in the above mentioned code, we have provided the complete path for the file which we also call as Absolute path. Now this code is not of much use unless we do something more along with it. Like after getting the reference for a file, we can see the size of the file, its parent folder, its name etc.

Check for the existence of an already created file

Sometimes we may be interested in knowing whether our file exists or not. For this, we use "FileExists" method. This checks whether the files exists or not. The "FileExists" method requires a single argument containing the full path of the file. This method returns a boolean value. I hope you guys must be aware of what a boolean value is. Now this method will return a value "True" if the file exists and "False" if the file doesn't exists.

The below mentioned code will explain in detail:

Set testobj = CreateObject("Scripting.FileSystemObject")
If test.FileExists("D:\Expert\MyTestFile.txt") Then
Set objFile = testobj.GetFile("D:\Expert\MyTestFile.txt")
Else
MsgBox "The file doesn't exist."
End If

Deleting the File

We can delete a file by firstly creating an instance of FileSystemObject and then we need to call the DeleteFile method. Lets see the below mentioned code to explain this concept.

Set myobj = CreateObject("Scripting.FileSystemObject")
myobj.DeleteFile("D:\MyTestFile.txt")

You can even delete your MS Word file with the above code. Cool na.... I know.

Let me show you something even more interesting. If the file is read-only, then the "DeleteFile" method will not be able to delete the file. So how to delete such a file? Any guesses.

This thing is left for you guys to find it out how we can delete a read only file.

Copy Files

Copying files from one location to another is also a very frequently done task. For this, we use CopyFile method. The syntax for copying files is:

obj.CopyFile "Source-File-Path", "Destination-File-Path"

In order to copy a file from "C:\test.txt" to "D:\test.txt", we will use the code:

Set obj=CreateObject("Scripting.FileSystemObject")
obj.CopyFile "C:\test.txt", "D:\test.txt"

We can even rename a file just by changing the name of the file in the "Destination-File-Path".

What happens if we try to copy a copy inside a folder and the folder doesnt exist? This thing is left for you guys to try.

Moving Files

We can also move files from one one location to another[Also known as Cut and Paste]. For this, we require MoveFile method. It is infact quite similar to CopyFile method.

We use the below code to move files from one location to another.

Set obj=CreateObject("Scripting.FileSystemObject")
obj.MoveFile "C:\test.txt", "D:\test.txt"

Reading all contents of a file in a single shot

We can read all the contents of a file using the ReadAll method. It captures all the file contents and stores inside a variable.
The following code will help you in reading all contents of a file "D:\test.txt".

Set obj1= CreateObject("Scripting.FileSystemObject")
Set obj2= obj1.OpenTextFile("D:\test.txt")
FileContents = obj2.ReadAll 'Read all contents of the file
MsgBox FileContents 'Display contents of file
obj2.Close 'Close the File

Reading all contents of a file line by line

We can read all the contents of a file line by line. For this we require the ReadLine method.
The following code will help you in reading all contents of a file "D:\test.txt" line by line.

Set obj1= CreateObject("Scripting.FileSystemObject")
Set obj2= obj1.OpenTextFile("D:\test.txt")
Do Until obj2.AtEndOfStream
Line=obj2.ReadLine
msgbox Line
Loop
obj2.Close

No comments:

 
Copyright © 2009 ExpertQTP