Pages

Wednesday, 19 October 2011

QTP Script: Example of how to read a text file line-by-line


' reading a file line by line

Const ForReading = 1

' create file system object
Set objFS = CreateObject("Scripting.FileSystemObject")

' first check that the file exists
If objFS.FileExists("c:\TextFile.txt"Then

    ' open the text file for reading
    Set objFile = objFS.OpenTextFile("c:\TextFile.txt"ForReadingFalse)

    ' do until at end of file
    Do Until objFile.AtEndOfStream

        ' store the value of the current line in the file
        strLine = objFile.ReadLine

        ' show the line from the file
        MsgBox strLine

    Loop ' next line

    ' close the file
    objFile.Close

    Set objFile = Nothing

Else ' file doesn't exist

    ' report a failure
    Reporter.ReportEvent micFail"Read File""File not found"

End if ' file exists

' destroy the objects
Set objFS = Nothing

0 comments:

Post a Comment