Monday, February 16, 2009

How to Read data from Text file with QTP?

We can read text line by line from text file with the below code.

Const ForReading = 1, ForWriting = 2
Dim fso, MyFile

' Create Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Create file and enter data into the created file
Set MyFile = fso.OpenTextFile("C:\SPQTP.txt", ForWriting, True)
MyFile.WriteLine "First line - - Hello world!"
MyFile.WriteLine "Second Line :-- "
MyFile.WriteLine "Third Line :-- "
MyFile.WriteLine "4th Line :-- "
MyFile.WriteLine "5th Line :-- "
MyFile.WriteLine "6th Line :-- "
MyFile.WriteLine "7th Line :-- "
MyFile.WriteLine "8th Line :-- "
MyFile.WriteLine "9th Line :-- "
MyFile.WriteLine "10th Line :-- "

' Close the file
MyFile.Close
' Open the file for reading
Set MyFile = fso.OpenTextFile("C:\SPQTP.txt", ForReading,True)

Do While not MyFile.AtEndOfStream
msgbox "Text :" & MyFile.Readline ' get data line by line
' msgbox "Text :" & MyFile.Read(15) ' get no of charecters from a line
' msgbox "Text :" & MyFile.ReadAll ' Get complete file text
Loop

No comments:

Post a Comment