WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

CSV

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Number of Records in a File


Question:

Is there a more direct means (besides counting with a loop) os knowing the number of records in a *.csv file? By number of records I mean number of separate records each of which contains a set of cs fields.

Answer:

BinaryStrCnt can be used the count the number of lines/ records in a file:
filename = "C:\Temp\Example.txt"
String = @LF ; number of lines / records
fs = FileSize(filename)
buffer = binaryalloc( fs )
BinaryRead(buffer,filename )
count = BinaryStrCnt( buffer, 0, fs-1, String)
;Check If the last line ends with a "lf" if so disregard. 
if binarypeekstr(buffer, fs-1, 1)!=String then count = count-1
BinaryFree( buffer )
Message( "Number of lines /records", count )

Another way might be:

filename = "C:\Test\Example.txt"
file = Fileget(filename)
file = strreplace(file,@crlf,@tab) ; assumes no tabs in the file already
records = itemcount(file,@tab)
if itemextract(-1,file,@tab) == "" then records = records - 1 ; last line followed by "crlf"
Message( "Number of lines /records", records)

Another way might be:

filename = "c:\temp\test.csv"
array = ArrayFileGetCsv(filename, 0, ",", 0, 0)
rowcount = ArrInfo(array,1)
colcount = ArrInfo(array,2)
Message(StrCat("Number of Rows in the file ",rowcount), StrCat("Number of Columns in the file ",colcount)) 

Article ID:   W16398
File Created: 2005:02:18:12:20:22
Last Updated: 2005:02:18:12:20:22