Number of lines in a file.

Adam150785
04-12-2003, 02:13 PM
Sorry to post this as a seperate post but if you dont you dont get a reply!

Is it possible to find out how many lines there are in a file?

Hope you can help!

Thanks
Adam.

Robse
04-12-2003, 02:22 PM
You can read the file into a string variable, then
use the Split() function with vbCrLf as delimiter to
fill another string array variable with the contents, and
the upper bound of that variable should hold the
number of lines. Note though that an empty line
means that there will be two vbCrLf's.


Dim Handle As Integer
Dim FileText As String
Dim SplitText() As String

Handle = FreeFile

Open YourFilePath For Binary As #Handle
FileText = Space$(LOF(Handle))
Get #Handle, , FileText
Close #Handle

SplitText = Split(FileText, vbCrLf)

MsgBox "There are " & UBound(SplitText) & " lines in " & YourFilePath


An easier way to do it would be to read the file in
with Line Input and just count the lines that way.

Adam150785
04-12-2003, 02:30 PM
Thanks m8 i think that code should do me just fine.

Adam150785
04-12-2003, 02:36 PM
would it be possible for you to explain every line of that code to me please so i have a better understanding?

Thanks
Adam.

Robse
04-12-2003, 02:57 PM
would it be possible for you to explain every line of that code to me please so i have a better understanding?

Thanks
Adam.

Mmmmph, not really.

What it does is it opens the file in binary mode, which is faster as
if it would be opened Random or Sequential mode.

This line

FileText = Space$(LOF(Handle))


pads the string variable "FileText" with as many characters as
there are in the file, to make sure that all of the file's contents
will be read into the variable, which happens in this line:


Get #Handle, , FileText


SplitText is declared as an array, and into it we put the contents
of the string variable "FileText", automatically "broken apart"
at each new line (vbCrLf) by the Split function:


SplitText = Split(FileText, vbCrLf)


The Ubound function returns the total amount of elements
within the array, which should be the number of lines.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum