Wednesday, August 15, 2007

How to Read Text File data from QTP

We can create File System Object for the text file and read the desired text from that file.Here is the function to perform that :


Function ReadText(Filepath,Row,ToSkip,ToRead)
Set fso = CreateObject("Scripting.FileSystemObject")
Set tso = fso.OpenTextFile(Filepath,1,false,0)

'For Reaching the desired ROW
If Row <> 1 Then
For i =1 to (Row-1)
tso.SkipLine
Next
End If

'Skip charecters as per required
tso.Skip(ToSkip)

'Read charecters as per required
ReadText = tso.Read(ToRead)

Set tso = Nothing
Set fso = Nothing

End Function


Call to function :
ReadText("C:\Test\TextFile.txt",3,4,5)

This function return the 5 charecters of the 3rd row after skipping 4 charecters of the File present at give path.

For example text file is as like this :

This is line 1.
We are in the second line of this file.
Welcome here is the third line of the text file we will end here we can go forward too.

Function will return a value = ome h

1 comment:

Mukeshsharma said...

Hi While running the function I get erro saying that invalid procedure call for ReadCSV = mid (RequiredLine,StartPosition+1,EndPosition-2)

I am using your ReadCSV File function.