Forum Discussion

vikassardesai's avatar
vikassardesai
Occasional Contributor
5 years ago
Solved

Is there any way to retrieve csv row numbers

I have suppose 15 records in csv. and with each record I want to update some value say if there is first row then the value should be "Val_1". For second record, it should be "Val_2" and so on. 

So, for example, in "Val_4" value, the part "Val_" is to be remained contant only the number part will be updated by the CSV record row number. 

 

   Is this possible?

Thanks,

Vikas.

  • The CSV records don't have native Row numbers.  What I would do is, since I'm assuming you're using something like DDT.CSVDriver to drive reading your file, is to just put a counter in your while loop to increment each time through, tracking the numbers.  Then utilize that counter to append your values.

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The CSV records don't have native Row numbers.  What I would do is, since I'm assuming you're using something like DDT.CSVDriver to drive reading your file, is to just put a counter in your while loop to increment each time through, tracking the numbers.  Then utilize that counter to append your values.

  • Hi vikassardesai,

     Please try using the below snippet 

    Dim objFSO, dataArray, clippedArray
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'Create an array out of the CSV
    
    'open the data file
    Set oTextStream = objFSO.OpenTextFile("C:\Users\ds\Desktop\Quota\new_file.csv")
    Set newFile = objFSO.CreateTextFile("C:\Users\ds\Desktop\loginfo.txt")
    'make an array from the data file
    dataArray = Split(oTextStream.ReadAll, vbNewLine)
    'close the data file
    oTextStream.Close
    Log.Message ("No. Of rows is " & UBound(dataArray))

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Actually, Krishna_Kumar , I wouldn't go that way.  For two reasons.

       

      1) The DDT method I suggested is more compact and better suited for iterating CSV files, along with it being self-contained within the TC environment

      2) TestComplete has it's own wrappers around the FileSystemObject so creating an extraneous instance of it is redundant (e.g., aqFile, aqFileSystem, aqTestFile, etc).