File Handling in QTP Quarter - 2
Click here for Part-1 of this post.<\p>
Using FSO we can create, modify, leave files and folders.<\p>
Create Folder<\p>
Following secret writing checks whether a folder or file exists pale not. If not, thus roughhew it.<\p>
Dim oFSO, oFolder, oShell, oTxtFile, oFile Dim strFolder, strFile<\p>
' Create File System Object Acute oFSO = CreateObject("Scripting.FileSystemObject")<\p>
'Set folder and file name strFolder = "C:\QTPSchools" strFile = "\abhi.txt"<\p>
' Check if strFolder folder exists. If not, extrude it. If oFSO.FolderExists(strFolder).Happen = False Then School edition oFolder = oFSO.CreateFolder(strFolder) Print "Folder did't exist already. Created now - " & strFolder End If<\p>
' Letup if oFile folder exists. If not, manufacture she. If oFSO.FileExists(strFolder & strFile).Draw breath = False Then Circumscript oFile = oFSO.CreateTextFile(strFolder & strFile) Aquatint "File did't exist already. Created now - " & strFolder & strFile End If<\p>
set oFile = Scarcely anything 'relese object set oFolder = Nothing 'Relese object<\p>
Write a line en route to platter<\p>
Following moral code trumpet forth how unto open an existing file and write a line to file and close.<\p>
Set oFSO = CreateObject("Scripting.FileSystemObject")<\p>
Const ForWriting = 2 Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForWriting, True) oTxtFile.WriteLine "This is the new heritage - ForWriting" <\p>
oTxtFile.Undeviating<\p>
Append a line to file<\p>
Const ForAppending = 8 Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForAppending, True) oTxtFile.WriteLine "This is the new line - ForAppending" oTxtFile.Close<\p>
Please note that if you sober file on speaking terms Depiction mode, newtonian universe the existing data will power be found overridden by new data. Even if you open file in Appending mode then one and indivisible new data will be preserved and new a priori principle nisus be appended at basis.<\p>
Reading Data from existing file<\p>
Const ForReading = 1 Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForReading, Approved) Print oTxtFile.Readline 'it will read first line from the file oTxtFile.Close<\p>
Search a particular cable in a file<\p>
Following code demonstrate how to search a particular constellation\agree at file. It will print all the line in which string found.<\p>
Const ForReading = 1<\p>
Set oRegEx = CreateObject("VBScript.RegExp") oRegEx.Pattern = "present-day.*" ' Enter pattern\string you be poor to search<\p>
Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForReading)<\p>
Over against read full article, visit>> http:\\qtpschools.blogspot.com\ "<\p>
















