I need to count the number of lines in a .txt file. I have managed to open the file, but could anyone advise me on a tidy way to do the counting please? - there are a lot of lines.
assume that your text file is mytest.txt located on C:
Dim nLine as Long
Dim Linedata As Variant
Open "c:\mytest.txt" For Input As #1
nLine = 0
Do While Not EOF(1)
nLine = nLine + 1
Line Input #1, Linedata
Loop
nLine = nLine + 1
MsgBox "Total line is " & Str(nLine)
in above code #1 is the file pointer. using Line Input command we skip 1 line & storing current line in variable called "Linedata". you can use Linedata variable to get what specific line contains.
Sorry about that. I assumed JJ1 was trying to get the line count without having to go through the whole file ("there are a lot of lines", and that the text file lines were a fixed length and you could just divide.
Thanks guys for the superb responses. In actual fact, you've both been really helpful, but just one more question!
The lines are, in fact, a fixed length of 8 characters (including spaces).
Can I now use "intLengthOfFile = LOF(1)"?
If yes, please could you tell me what figure I would divide by, given that each line contains 8 characters, in order to calculate the number of lines?
You will obviously have to test this yourself on files of different sizes, but if you take
(LOF + 2) / (numcharsinline + 2)
it should work. For instance, if I have 30 lines of 10 characters in a file, I'll get 358 as the LOF because there are 300 characters + 29 sets of carriage return/line feeds worth 2 each. The last line does not have cr/lf, so you add 2 to make up for that, making the top 360. Then you take the number of characters in the line (10) plus the cr/lf (2), making 12, and that is the bottom.
Public Function basNLines(fName As String, _
Optional RecSep As String = vbCrLf) As Variant
'fName is the Fully Qualified Path
Dim Fil As Integer
Dim RawFile As String 'Holds the entire contents of the file
Dim RawSplit() As String 'the file split up on a line per line basis
Fil = FreeFile 'get the next free file number
Open fName For Binary As #Fil 'Open file
RawFile = String$(LOF(Fil), 32) 'Create "empty" String of Length
Get #Fil, 1, RawFile 'Fill "Empty Str with File
Close #Fil 'Close File
'Get the Nunber of Records and Fields
RawSplit = Split(RawFile, RecSep) 'Split the file up by lines
basNLines = UBound(RawSplit) - 1 'Num Lines/Records in the file
End Function
MichaelRed
m.red@att.net
There is never time to do it right but there is always time to do it over
ipmcarl(progammer)
Maybe I am missing something. Won't
mysize=lof(1)/ record length work?
if you have a fixed record length and lof is the
total size of the file, it should return the number
of records, which in this case is the number of lines
Well, it may -or it may not. Different 'programs' use different terminators, so ALWAYS padding the reclen by two will not always give the correct answer, Further, you need to KNOW the record length to begin with. BY reading and splitting the text file into an array of strings, neither the reclen or the terminiator needs to be known in advance.
Another issue is simply that knowing the number of 'records' is seldom the end of the process, but is more often used as an item of information is a more complete process. Placing the records in a text array leaves them available for the remainder of the processing.
MichaelRed
m.red@att.net
There is never time to do it right but there is always time to do it over
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.