Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a file Line Count

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
0
0
US
I am adding a needed feature to an already existing program that is written in VB6.

I am trying to count lines in large files (3.5 gigs).

Does VB6 have a function that will do this for me as efficient as the shell command Find or GREP?

Or does anyone have a better solution?

Currently, I have made a class that runs GREP from the command line in VB.NET and made this an available reference for my VB6 project, which returns the line count output from the command line. This has its issues with where I need to contain the executable which is not desireable.

Any help would be greatly appreciated.

Thanks in advance.
 
Why not simply open the file for reading and while not EOF do a incement count.

Ex.

Number = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(AppPath & "TextFile.txt", 1)
Do Until objFile.AtEndofStream
strTemp = objFile.ReadLine
Number = Number + 1
loop

Number will hold the line count once this runs. Hope this helps.

Jeffery Smith (Smitty)
PEC Solutions Inc.
A+ Network+ MCSA MCSE
 
I tried that...

I think it is still running. LOL....

A 3.5 Gig file takes forever with filesystemobject not to mention hammered the Xeon CPU . Also while testing this approach it failed a few times on the readline.

When I use FIND or GREP it takes seconds.

Thanks for the input...
 
Don't use file system object.

Try something like this...

dim lLines as Long
dim iFile as Integer
dim sLine as String

iFile = freefile()
lLines = 0
Open "myfile.txt" for input as iFile
Do Until eof(iFile)
Line Input #iFile, sLine
lLines = lLines + 1
Loop
Close iFile

Debug.Print cstr(lLines)
 
Is your file fixed fielded? If it is, just find the size of the file in bytes and divide it by the record size. If the size if your lines vary then this method will not work. You will probably have to use FSO or the API to return the file size due to its sheer size. I don't think that FileLen or LOF will suffice as I think that they have file size limitations. Hope this helps.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top