Micro Focus QTP (UFT) Forums
Retrieving data from Txt file into datatable - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Retrieving data from Txt file into datatable (/Thread-Retrieving-data-from-Txt-file-into-datatable)



Retrieving data from Txt file into datatable - kriday - 07-16-2013

Hi
I have written the following code to get data from txt file into datatable
Code:
Const   ForReading =1
row=1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ExcelFiles\TestReg.txt" ,  ForReading)

Dim afilelines()
i=0
Do until objFile.AtEndOfStream
    ReDim Preserve afilelines(i)
    afilelines(i) = objFile.ReadLine
    i=i+1
Loop

objFile.Close
For Each strLine in afilelines
    MyArray =Split(strLine, " ", -1,1)
    For each sline in MyArray
        DataTable.SetCurrentRow(row)
        datatable("A" , dtglobalsheet) = sline
        row=row+1
    Next
Next
DataTable.ExportSheet  "C:\ExcelFiles\Book1.xls" , 1
ExitRun
but iam getting the following error

Code:
The set DataTable.Value operation failed. The <A> column does not exist.

Line (19): "DataTable("A" , dtGlobalSheet) = sline".

I need help on this
Thank you all


RE: Retrieving data from Txt file into datatable - vinod123 - 07-17-2013

i am not able know the error you have committed so i have posting the code which i use
Code:
Public Function ReadLineTextFile (strTextFile)

Dim fso, MyFile
Dim arrPairs, arrColumns, i

  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Open the file for reading only
  Set MyFile = fso.OpenTextFile(strTextFile, 1)

  ' Read the input file
  ReadLineTextFile = MyFile.ReadLine

  ' Load the Global DataSheet.
  arrPairs = Split(ReadLineTextFile, "|")

  ' Note: This loop begins at Index 3 because Indices 0, 1, and 2
  '      (CurrentURL, CurrentBrowser, and CurrentPage) are already
  '      in the Global DataSheet)
  For i = 3 To UBound(arrPairs) - 1
    arrColumns = Split(arrPairs(i), ";")
    DataTable.GlobalSheet.AddParameter arrColumns(0), ""
    DataTable.Value(arrColumns(0), dtGlobalSheet) = arrColumns(1)
  Next

End Function '  ReadLineTextFile (strTextFile)

In your test script, just identify the path and filename of the text file...

  ' Identify the name of the input file to use.
  strTextFilePath = "C:\Test Data Files\"
  strTextFile = strTextFilePath & "FileName.txt"

Then just call the function when you need to...

  Call ReadLineTextFile (strTextFile)



RE: Retrieving data from Txt file into datatable - srinijg20 - 07-19-2013

Hi

Did u rename the column name as "A" if not rename it.


RE: Retrieving data from Txt file into datatable - vinod123 - 07-23-2013

just rename the column name to A