|
-
February 2nd, 2000, 04:47 AM
#1
Counting the records in a file
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
-
February 7th, 2000, 08:50 AM
#2
Re: Counting the records in a file
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.
-
February 7th, 2000, 11:20 AM
#3
Re: Counting the records in a file
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
[email protected]
[email protected]
-
February 8th, 2000, 12:21 AM
#4
Re: Counting the records in a file
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|