For QTP Scripts & Documents Visit: [Link].
com
Working with Files
I) Computer file System
In computing, a file system (often also written as filesystem) is a method for storing
and organizing computer files and the data they contain to make it easy to find and
access them. File systems may use a data storage device such as a hard disk or CD-
ROM.
II) Working with Drives and Folders
a) Creating a Folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\logs"
Set objFSO = CreateObject("[Link]")
Set objFolder = [Link](strDirectory)
b) Deleting a Folder
Set oFSO = CreateObject("[Link]")
[Link]("E:\FSO")
c) Copying Folders
Set oFSO=createobject("[Link]")
[Link] "E:\gcr6", "C:\jvr", True
d) Checking weather the folder available or not, if not creating the folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\logs"
Set objFSO = CreateObject("[Link]")
If [Link](strDirectory) Then
Set objFolder = [Link](strDirectory)
msgbox strDirectory & " already created "
else
Set objFolder = [Link](strDirectory)
end if
e) Returning a collection of Disk Drives
Set oFSO = CreateObject("[Link]")
Set colDrives = [Link]
For Each oDrive in colDrives
MsgBox "Drive letter: " & [Link]
Next
f) Getting available space on a Disk Drive
For Software Testing Documents visit: [Link] 1
For QTP Scripts & Documents Visit: [Link]
Set oFSO = CreateObject("[Link]")
Set oDrive = [Link]("C:")
MsgBox "Available space: " & [Link]
III) Working with Flat Files
a) Creating a Flat File
Set objFSO = CreateObject("[Link]")
Set objFile = [Link]("E:\[Link]")
b) Checking weather the File is available or not, if not creating the File
strDirectory="E:\"
strFile="[Link]"
Set objFSO = CreateObject("[Link]")
If [Link](strDirectory & strFile) Then
Set objFolder = [Link](strDirectory)
Else
Set objFile = [Link]("E:\[Link]")
End if
c) Reading Data character by character from a Flat File
Set objFSO = CreateObject("[Link]")
Set objFile = [Link]("E:\[Link]", 1)
Do Until [Link]
strCharacters = [Link](1)
msgbox strCharacters
Loop
d) Deleting Data From a Flat File
e) Comparing Flat Files
f) Searching Particular Strings in a Flat File
d) Reading Data line by line from a Flat File
Set objFSO = CreateObject("[Link]")
Set objFile = [Link]("E:\[Link]", 1)
Do Until [Link]
strCharacters = [Link]
msgbox strCharacters
Loop
e) Reading data from a flat file and using in data driven testing
Dim fso,myfile
Set fso=createobject("[Link]")
Set myfile= [Link] ("F:\[Link]",1)
[Link]
While [Link] <> True
x=[Link]
s=split (x, ",")
For Software Testing Documents visit: [Link] 2
For QTP Scripts & Documents Visit: [Link]
[Link] "C:\Program Files\Mercury Interactive\QuickTest
Professional\samples\flight\app\[Link]","","C:\Program Files\Mercury
Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set s(0)
Dialog("Login").WinEdit("Password:").SetSecure s(1)
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Wend
f) Writing data to a text file
Dim Stuff, myFSO, WriteStuff, dateStamp
dateStamp = Date()
Stuff = "I am Preparing this script: " &dateStamp
Set myFSO = CreateObject("[Link]")
Set WriteStuff = [Link]("e:\[Link]", 8, True)
[Link](Stuff)
[Link]
SET WriteStuff = NOTHING
SET myFSO = NOTHING
g) Delete a text file
Set objFSO=createobject("[Link]")
Set txtFilepath = [Link]("E:\[Link]")
[Link]()
h) Checking weather the File is available or not, if available delete the File
strDirectory="E:\"
strFile="[Link]"
Set objFSO = CreateObject("[Link]")
If [Link](strDirectory & strFile) Then
Set objFile = [Link](strDirectory & strFile)
[Link] ()
End if
i) Comparing two text files
Dim f1, f2
f1="e:\[Link]"
f2="e:\[Link]"
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject("[Link]")
If [Link](FilePath1).Size <> [Link](FilePath2).Size Then
CompareFiles = True
Exit Function
For Software Testing Documents visit: [Link] 3
For QTP Scripts & Documents Visit: [Link]
End If
Set File1 = [Link](FilePath1).OpenAsTextStream(1, 0)
Set File2 = [Link](FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While [Link] = False
Str1 = [Link]
Str2 = [Link]
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles <> 0 Then
CompareFiles = True
Exit Do
End If
Loop
[Link]()
[Link]()
End Function
Call Comparefiles(f1,f2)
If CompareFiles(f1, f2) = False Then
MsgBox "Files are identical."
Else
MsgBox "Files are different."
End If
j) Counting the number of times a word appears in a file
sFileName="E:\[Link]"
sString="gcreddy"
Const FOR_READING = 1
Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatches
Set oFso = CreateObject("[Link]")
Set oTxtFile = [Link](sFileName, FOR_READING)
sReadTxt = [Link]
Set oRegEx = New RegExp
[Link] = sString
[Link] = bIgnoreCase
[Link] = True
Set oMatches = [Link](sReadTxt)
MatchesFound = [Link]
Set oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothing
msgbox MatchesFound
IV) Working with Word Docs
a) Create a word document and enter some data & save
Dim objWD
Set objWD = CreateObject("[Link]")
For Software Testing Documents visit: [Link] 4
For QTP Scripts & Documents Visit: [Link]
[Link]
[Link] "This is some text." & Chr(13) & "This is some more text"
[Link] "e:\[Link]"
[Link]
V) Working with Excel Sheets
a) Create an excel sheet and enter a value into first cell
Dim objexcel
Set objExcel = createobject("[Link]")
[Link] = True
[Link]
[Link](1, 1).Value = "Testing"
[Link]("f:\[Link]")
[Link]
b) Compare two excel files
Set objExcel = CreateObject("[Link]")
[Link] = True
Set objWorkbook1= [Link]("E:\[Link]")
Set objWorkbook2= [Link]("E:\[Link]")
Set objWorksheet1= [Link](1)
Set objWorksheet2= [Link](1)
For Each cell In [Link]
If [Link] <> [Link]([Link]).Value Then
msgbox "value is different"
Else
msgbox "value is same"
End If
Next
[Link]
[Link]
[Link]
set objExcel=nothing
For Software Testing Documents visit: [Link] 5