Click to See Complete Forum and Search --> : Counting the records in a file


venkytvs
February 2nd, 2000, 03:47 AM
Hi all
Can anybody give me an idea how to cnt the number or records/lines in a txtfile without reading the file.

Thanks in advance
Venky

johnpc1
February 7th, 2000, 07:50 AM
I don't think so. You would have to open the file
and set up a count varible in a For Next Loop to
count the recrods and then close the file.

Aaron Young
February 7th, 2000, 10:20 AM
You would have to Open the File but here's a Quick way to Count Records if you have VB6, using the Replace Function, if you don't have VB6, you can write a Replace Equivelant Function:
private Sub Command1_Click()
Caption = CountRecords("C:\TheFile.txt")
End Sub

Function CountRecords(byval FileName as string, optional byval Delimiter as string = vbCrLf) as Long
Dim iFile as Integer
Dim lFileLen as Long
Dim lChunk as Long
Dim sChunk as string
Dim sData as string

'Open the File and Load it into a string Variable..
iFile = FreeFile
Open FileName for binary Access Read as iFile
lFileLen = LOF(iFile)
lChunk = 64000
While Loc(iFile) < lFileLen
If Loc(iFile) + lChunk > lFileLen then lChunk = lFileLen - Loc(iFile)
sChunk = Space(lChunk)
get #iFile, , sChunk
sData = sData & sChunk
Wend
Close iFile
'Quickly Retrieve the No. Of Records by Removing all Record Delimiters and
'Comparing the Length of the File Before and After..
CountRecords = (lFileLen - len(Replace(sData, Delimiter, ""))) / len(Delimiter)

End Function



Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com

venkytvs
February 7th, 2000, 11:21 PM
Hi Aron, Jhonpc

Thanks for ur replies.
I tried these methods but it is very slow as I have to read 1000 files with each file containing more than 1000 records. If u know any other method please reply back.

Thanks
Venky