What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleCount the lines in a text file
Keywordscount lines, text file
CategoriesFiles and Directories
Read the file a line at a time and return the number of lines.
Private Function LinesInFile(ByVal file_name As String) As _
Dim fnum As Integer
Dim lines As Long
Dim one_line As String

    fnum = FreeFile
    Open file_name For Input As #fnum
    Do While Not EOF(fnum)
        Line Input #fnum, one_line
        lines = lines + 1
    Close fnum

    LinesInFile = lines
End Function
This program uses disk, directory, and file list boxes to make selecting files easy.

WJK says:

The function "lines in file" is a text file line counter and works, but I think we want lines of code.... so a blank line should not be counted. Also, a remark line should not be counted. I might get some disagreement on this. Of course I consider remarks VERY important, they are not code and irrelevant to the compiler.

What about lines of code that span multiple text lines? This is not picked up with this routine. Guess it all gets down to ones definition of line count. When all is Said and done, what can the figure actually be used for??

He's right about it all coming down to point of view.

I've worked in organizations where they carefully defined all this because they tied your maintenance budget to lines of code. They didn't count comments or blank space and counted continued lines as one statement. So you could find code like this:

    x = x + 1
    x = x + 1
    x = x + 1

Instead of

    x = x + 3

All their careful definitions didn't stop programmers from abusing the system.

People have argued that the real way to measure quantity of software is by counting function points. These are more or less places where the code can branch. You can fool that, too.

Overall I just use a general line count to give me an idea of how much code I have to maintain. And I consider comments and white space used for formatting to be an important part of the code.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.