• No results found

File System OperationsFile System Operations

In document Advanced+QTP+Open+Order (Page 36-41)

File System Operations

I) Working with Drives and Folders I) Working with Drives and Folders

a) Creating a Folder a) Creating a Folder Option Explicit

Option Explicit

Dim objFSO, objFolder, strDirectory Dim objFSO, objFolder, strDirectory strDirectory = "D:\logs"

strDirectory = "D:\logs"

Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") Set objFolder

Set objFolder = objFSO.CreateFolder(strDirectory)= objFSO.CreateFolder(strDirectory) b) Deleting a Folder

b) Deleting a Folder Set oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject"= CreateObject("Scripting.FileSystemObject")) oFSO.DeleteFolder("E:\FSO")

oFSO.DeleteFolder("E:\FSO") c) Copying Folders

c) Copying Folders Set

Set oFSO=createobjecoFSO=createobject("Scripting.Filesystemobt("Scripting.Filesystemobject")ject") oFSO.CopyFolder "E:\gcr6", "C:\jvr", True

oFSO.CopyFolder "E:\gcr6", "C:\jvr", True

d) Checking weather the folder available or not, if not creating the folder d) Checking weather the folder available or not, if not creating the folder Option Explicit

Option Explicit

Dim objFSO, objFolder, strDirectory Dim objFSO, objFolder, strDirectory strDirectory = "D:\logs"

strDirectory = "D:\logs"

Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") If

If objFSO.FolderExisobjFSO.FolderExists(strDirectory) Thents(strDirectory) Then Set objFolder =

Set objFolder = objFSO.GetFolder(stobjFSO.GetFolder(strDirectory)rDirectory) msgbox strDirectory & " already created "

msgbox strDirectory & " already created "

else else

Set objFolder

Set objFolder = objFSO.CreateFolder(strDirectory)= objFSO.CreateFolder(strDirectory) end if 

end if 

e) Returning a collection of Disk Drives e) Returning a collection of Disk Drives Set oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject"= CreateObject("Scripting.FileSystemObject")) Set colDrives = oFSO.Drives

Set colDrives = oFSO.Drives For Each oDrive in colDrives For Each oDrive in colDrives

MsgBox "Drive letter: " & oDrive.DriveLetter MsgBox "Drive letter: " & oDrive.DriveLetter Next

Next

f) Getting available space on a Disk Drive f) Getting available space on a Disk Drive Set oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject"= CreateObject("Scripting.FileSystemObject")) Set oDrive =

Set oDrive = oFSO.GetDrive("C:oFSO.GetDrive("C:")") MsgBox "Available space: " &

MsgBox "Available space: " & oDrive.AvailableSpaceoDrive.AvailableSpace

II) Working with Flat Files II) Working with Flat Files

Creating a Flat File Creating a Flat File

Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") Set objFile

Set objFile = objFSO.CreateTextFile("E:\ScriptLog.= objFSO.CreateTextFile("E:\ScriptLog.txt")txt") b)

b) Checking weather the File is available or not, if not creating the FileChecking weather the File is available or not, if not creating the File strDirectory="E:\"

strDirectory="E:\"

strFile="Scripting.txt"

strFile="Scripting.txt"

Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strDirectory & strFile) Then

If objFSO.FileExists(strDirectory & strFile) Then Set objFolder =

Set objFolder = objFSO.GetFolder(objFSO.GetFolder(strDirectory)strDirectory) Else

Else

Set objFile

Set objFile = objFSO.CreateTextFile("E:\ScriptLog.= objFSO.CreateTextFile("E:\ScriptLog.txt")txt") End if 

End if 

Reading Data character by character from a Flat File Reading Data character by character from a Flat File

Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") Set objFile =

Set objFile = objFSO.OpenTextFile(objFSO.OpenTextFile("E:\gcr.txt", 1)"E:\gcr.txt", 1) Do Until

Do Until objFile.AtEndOfStreamobjFile.AtEndOfStream strCharacters = objFile.Read(1) strCharacters = objFile.Read(1) msgbox strCharacters

msgbox strCharacters Loop

Loop

d) Reading Data line by line from a

d) Reading Data line by line from a Flat FileFlat File Set objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") Set objFile =

Set objFile = objFSO.OpenTextFile(objFSO.OpenTextFile("E:\gcr.txt", 1)"E:\gcr.txt", 1) Do Until

Do Until objFile.AtEndOfStreamobjFile.AtEndOfStream strCharacters = objFile.Readline strCharacters = objFile.Readline msgbox strCharacters

msgbox strCharacters Loop

Loop

e) Reading data from a flat file

e) Reading data from a flat file and using in data driven testingand using in data driven testing Dim fso,myfile

Dim fso,myfile Set

Set fso=createobject(fso=createobject("scripting.filesystemobj"scripting.filesystemobject")ect") Set myfile=

Set myfile= fso.opentextfile ("F:\gcr.txt",1)fso.opentextfile ("F:\gcr.txt",1) myfile.skipline

myfile.skipline

While myfile.atendofline <> True While myfile.atendofline <> True x=myfile.readline

x=myfile.readline s=split (x, ",") s=split (x, ",")

SystemUtil.Run "C:\Program

SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTestFiles\Mercury Interactive\QuickTest Professional\samples\

Professional\samples\flight\app\flight4a.exe","","C:\Progflight\app\flight4a.exe","","C:\Program ram Files\MercuryFiles\Mercury Interactive\QuickTest

Interactive\QuickTest Professional\sProfessional\samples\flight\app\","open"amples\flight\app\","open"

Dialog("Login").Activate Dialog("Login").Activate

Dialog("Login").WinEdit("Agent Name:").Set s(0) Dialog("Login").WinEdit("Agent Name:").Set s(0) Dialog("Login").WinEdit("Passw

Dialog("Login").WinEdit("Password:").SetSecure ord:").SetSecure s(1)s(1) Dialog("Login").WinButton("OK").Click

f) Writing data to a text file f) Writing data to a text file

Dim Stuff, myFSO, WriteStuff, dateStamp Dim Stuff, myFSO, WriteStuff, dateStamp dateStamp = Date()

dateStamp = Date() Stuff = "I am

Stuff = "I am Preparing this script: " &dateStampPreparing this script: " &dateStamp Set myFSO

Set myFSO = CreateObject("Scripting.FileSystemObj= CreateObject("Scripting.FileSystemObject")ect") Set WriteStuff =

Set WriteStuff = myFSO.OpenTextFile(myFSO.OpenTextFile("e:\gcr.txt", 8, True)"e:\gcr.txt", 8, True) WriteStuff.WriteLine(Stuff)

WriteStuff.WriteLine(Stuff) WriteStuff.Close

WriteStuff.Close

SET WriteStuff = NOTHING SET WriteStuff = NOTHING SET myFSO = NOTHING SET myFSO = NOTHING g) Delete a text file g) Delete a text file Set

Set objFSO=createobjobjFSO=createobject("Scripting.filesystect("Scripting.filesystemobject")emobject") Set txtFilepath =

Set txtFilepath = objFSO.GetFile("E:\objFSO.GetFile("E:\gcr.txt")gcr.txt") txtFilepath.Delete()

txtFilepath.Delete()

h) Checking weather the File is available or not, if available

h) Checking weather the File is available or not, if available delete thedelete the File

Set objFSO = CreateObject("Scripting.FileSystemObject")= CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strDirectory & strFile) Then

If objFSO.FileExists(strDirectory & strFile) Then Set objFile =

Set objFile = objFSO.Getfile(strDirecobjFSO.Getfile(strDirectory & strFile)tory & strFile) objFile.delete ()

objFile.delete () End if 

End if 

i) Comparing two text files i) Comparing two text files Dim f1, f2

Public Function CompareFiles (FilePath1, FilePath2) Public Function CompareFiles (FilePath1, FilePath2) Dim FS, File1, File2

Dim FS, File1, File2 Set FS

Set FS = CreateObject("Scripting.FileSystemObj= CreateObject("Scripting.FileSystemObject")ect")

If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then CompareFiles = True

Set File1 = FS.GetFile(FilePath1).OpenAFS.GetFile(FilePath1).OpenAsTextStream(1, 0)sTextStream(1, 0) Set File2 =

Set File2 = FS.GetFile(FilePath2).OpenAFS.GetFile(FilePath2).OpenAsTextStream(1, 0)sTextStream(1, 0)

CompareFiles = False CompareFiles = False

Do While File1.AtEndOfStream = False Do While File1.AtEndOfStream = False Str1 = File1.Read

Str1 = File1.Read Str2 = File2.Read Str2 = File2.Read

CompareFiles = StrComp(Str1, Str2, 0) CompareFiles = StrComp(Str1, Str2, 0) If CompareFiles <> 0 Then

If CompareFiles <> 0 Then CompareFiles = True

If CompareFiles(f1, f2) = False Then If CompareFiles(f1, f2) = False Then MsgBox "Files are identical."

MsgBox "Files are identical."

Else Else

MsgBox "Files are different."

MsgBox "Files are different."

End If  End If 

 j) Counting the number of times a word appears in a file  j) Counting the number of times a word appears in a file

sFileName="E:\gcr.txt"

sFileName="E:\gcr.txt"

sString="Tester"

sString="Tester"

Const FOR_READING = 1 Const FOR_READING = 1

Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatches Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatches Set oFso

Set oFso = CreateObject("Scripting.FileSystemObj= CreateObject("Scripting.FileSystemObject")ect") Set oTxtFile =

Set oTxtFile = oFso.OpenTextFile(soFso.OpenTextFile(sFileName, FOR_READING)FileName, FOR_READING) sReadTxt = oTxtFile.ReadAll

sReadTxt = oTxtFile.ReadAll Set oRegEx = New RegExp Set oRegEx = New RegExp oRegEx.Pattern = sString

Set oMatches = oRegEx.Execute(sReadTxt)oRegEx.Execute(sReadTxt) MatchesFound = oMatches.Count

MatchesFound = oMatches.Count

Set oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothing Set oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothing msgbox MatchesFound

msgbox MatchesFound

III) Working with Word Docs III) Working with Word Docs

a) Create a word document and enter some data & save a) Create a word document and enter some data & save Dim objWD

Dim objWD Set objWD =

Set objWD = CreateObject("Word.AppCreateObject("Word.Application")lication") objWD.Documents.Add

objWD.Documents.Add

objWD.Selection.TypeText

objWD.Selection.TypeText "This is some text." & C"This is some text." & Chr(13) & "This is shr(13) & "This is some moreome more text"

text"

objWD.ActiveDocum

objWD.ActiveDocument.SaveAs ent.SaveAs "e:\Tester.doc""e:\Tester.doc"

objWD.Quit objWD.Quit

IV) Working with Excel Sheets IV) Working with Excel Sheets

a) Create an excel sheet and enter a value into first a) Create an excel sheet and enter a value into first cellcell Dim objexcel

Dim objexcel Set objExcel =

Set objExcel = createobject("Excreateobject("Excel.application")cel.application") objexcel.Visible = True

objexcel.Visible = True objexcel.Workbooks.add objexcel.Workbooks.add

objexcel.Cells(1, 1).Value = "Testing"

objexcel.Cells(1, 1).Value = "Testing"

objexcel.ActiveWorkbook.SaveAs("f:\Tester1.xls") objexcel.ActiveWorkbook.SaveAs("f:\Tester1.xls") objexcel.Quit

objexcel.Quit

b) Compare two excel files b) Compare two excel files Set objExcel =

Set objExcel = CreateObject("ExcCreateObject("Excel.Application")el.Application") objExcel.Visible = True

objExcel.Visible = True Set

Set objWorkbook1= objExcel.Workbooks.Open("E:\objWorkbook1= objExcel.Workbooks.Open("E:\gcr1.xls")gcr1.xls") Set

Set objWorkbook2= objExcel.Workbooks.Open("E:\objWorkbook2= objExcel.Workbooks.Open("E:\gcr2.xls")gcr2.xls") Set

Set objWorksheet1= objWorkbook1.Worksheets(objWorksheet1= objWorkbook1.Worksheets(1)1) Set

Set objWorksheet2= objWorkbook2.Worksheets(objWorksheet2= objWorkbook2.Worksheets(1)1) For Each cell

For Each cell In objWorksheet1.UsedRangeIn objWorksheet1.UsedRange If cell.Value

If cell.Value <> objWorksheet2.Range(cell.A<> objWorksheet2.Range(cell.Address).Value Thenddress).Value Then msgbox "value is different"

msgbox "value is different"

Else Else

msgbox "value is same"

msgbox "value is same"

End If  End If  Next Next

objWorkbook1.close objWorkbook1.close objWorkbook2.close objWorkbook2.close objExcel.quit

objExcel.quit set

set objExcel=nothinobjExcel=nothingg

In document Advanced+QTP+Open+Order (Page 36-41)

Related documents