Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCount the lines of code in a Visual Basic 5/6 project
DescriptionThis example shows how to count the lines of code in a Visual Basic 5/6 project.
Keywordscount lines, project, forms, modules, subs, subroutines, functions
CategoriesSoftware Engineering
 
This program lets you select a .VBP file. It opens the file and finds the frm, bas, and cls files it uses.

The program then opens those files. It finds the start and finish of the frm files' control declarations and it counts the subroutines, functions, properties, and lines in the rest of the file.

This is a fairly simplistic method that I threw together to give me an idea of how many lines of code I was managing. You could make the program much smarter and have it count control types, code in event handlers, etc. Let me know if you make improvements.

 
' Return a report on a project.
Private Function ReportOnProject(ByVal project_file As _
    String) As String
Dim project_dir As String
Dim fnum As Integer
Dim new_line As String
Dim project_forms As Collection
Dim project_modules As Collection
Dim project_classes As Collection
Dim i As Integer
Dim txt As String
Dim total_lines As Long
Dim total_controls As Long
Dim total_subs As Long
Dim total_functions As Long
Dim total_properties As Long
Dim total_comments As Long

    ' Get the project directory.
    project_dir = Left$(project_file, _
        InStrRev(project_file, "\"))

    ' Process the project file.
    fnum = FreeFile
    Open project_file For Input As fnum

    Set project_forms = New Collection
    Set project_modules = New Collection
    Set project_classes = New Collection

    ' Read the lines in the project file.
    Do While Not EOF(fnum)
        ' Read a line.
        Line Input #fnum, new_line

        ' See what this line is.
        If PrefixMatches(new_line, TOKEN_FORM) Then
            ' It's a form.
            project_forms.Add Mid$(new_line, _
                Len(TOKEN_FORM) + 1)
        ElseIf PrefixMatches(new_line, TOKEN_MODULE) Then
            ' It's a module.
            project_modules.Add Trim$(Mid$(new_line, _
                InStr(new_line, ";") + 1))
        ElseIf PrefixMatches(new_line, TOKEN_CLASS) Then
            ' It's a class.
            project_classes.Add Trim$(Mid$(new_line, _
                InStr(new_line, ";") + 1))
        End If
    Loop

    ' Close the project file.
    Close fnum

    ' Sort the collections.
    SortCollection project_forms
    SortCollection project_modules

    ' Start the report.
    txt = "Project:    " & project_file & vbCrLf

    ' Process the forms.
    If project_forms.Count > 0 Then
        txt = txt & vbCrLf
        txt = txt & "*************" & vbCrLf
        txt = txt & "*** FORMS ***" & vbCrLf
        txt = txt & "*************" & vbCrLf
        For i = 1 To project_forms.Count
            txt = txt & vbCrLf & ReportOnForm(project_dir, _
                project_forms(i), total_lines, _
                total_controls, total_subs, _
                total_functions, total_properties, _
                total_comments)
        Next i
    End If

    ' Process the modules.
    If project_modules.Count > 0 Then
        txt = txt & vbCrLf
        txt = txt & "***************" & vbCrLf
        txt = txt & "*** MODULES ***" & vbCrLf
        txt = txt & "***************" & vbCrLf
        For i = 1 To project_modules.Count
            txt = txt & vbCrLf & _
                ReportOnModule(project_dir, _
                project_modules(i), "Module", total_lines, _
                total_controls, total_subs, _
                total_functions, total_properties, _
                total_comments)
        Next i
    End If

    ' Process the classes.
    If project_classes.Count > 0 Then
        txt = txt & vbCrLf
        txt = txt & "***************" & vbCrLf
        txt = txt & "*** CLASSES ***" & vbCrLf
        txt = txt & "***************" & vbCrLf
        For i = 1 To project_classes.Count
            txt = txt & vbCrLf & _
                ReportOnModule(project_dir, _
                project_classes(i), "Class", total_lines, _
                total_controls, total_subs, _
                total_functions, total_properties, _
                total_comments)
        Next i
    End If

    ' Return the result.
    txt = txt & vbCrLf
    txt = txt & "***************" & vbCrLf
    txt = txt & "*** SUMMARY ***" & vbCrLf
    txt = txt & "***************" & vbCrLf
    txt = txt & vbCrLf
    txt = txt & "Forms:            " & _
        Format$(project_forms.Count, "@@@@@@@") & vbCrLf
    txt = txt & "Modules:          " & _
        Format$(project_modules.Count, "@@@@@@@") & vbCrLf
    txt = txt & "Classes:          " & _
        Format$(project_classes.Count, "@@@@@@@") & vbCrLf
    txt = txt & "Total Controls:   " & _
        Format$(total_controls, "@@@@@@@") & vbCrLf
    txt = txt & "Total Lines:      " & Format$(total_lines, _
        "@@@@@@@") & vbCrLf
    txt = txt & "Total Subs:       " & Format$(total_subs, _
        "@@@@@@@") & vbCrLf
    txt = txt & "Total Functions:  " & _
        Format$(total_functions, "@@@@@@@") & vbCrLf
    txt = txt & "Total Properties: " & _
        Format$(total_properties, "@@@@@@@") & vbCrLf
    txt = txt & "Total Comments:   " & _
        Format$(total_comments, "@@@@@@@") & vbCrLf

    ReportOnProject = txt
End Function

' Return a report about a form.
Private Function ReportOnForm(ByVal project_dir As String, _
    ByVal form_file As String, ByRef total_lines As Long, _
    ByRef total_controls As Long, ByRef total_subs As Long, _
    ByRef total_functions As Long, ByRef total_properties _
    As Long, ByRef total_comments As Long) As String
Dim txt As String
Dim fnum As Integer
Dim new_line As String
Dim begins_open As Long
Dim num_controls As Long
Dim num_lines As Long
Dim num_functions As Long
Dim num_subs As Long
Dim num_properties As Long
Dim num_Comments As Long

    txt = "Form:       " & form_file & vbCrLf

    fnum = FreeFile
    On Error Resume Next
    Open project_dir & form_file For Input As fnum
    If Err.Number <> 0 Then
        txt = txt & "*** Error " & _
            Format$(Err.Number) & _
            " opening form file " & _
            project_dir & form_file & _
            vbCrLf & Err.Description
        ReportOnForm = txt
        Exit Function
    End If
    On Error GoTo 0

    ' Find the "Begin VB.Form" line.
    Do While Not EOF(fnum)
        Line Input #fnum, new_line
        new_line = Trim$(new_line)
        If PrefixMatches(new_line, TOKEN_BEGIN) _
            Then Exit Do
    Loop

    ' Search for the corresponding End.
    begins_open = 1
    Do While Not EOF(fnum)
        Line Input #fnum, new_line
        new_line = Trim$(new_line)
        If PrefixMatches(new_line, TOKEN_BEGIN) Then
            num_controls = num_controls + 1
            begins_open = begins_open + 1
        ElseIf PrefixMatches(new_line, TOKEN_END) Then
            begins_open = begins_open - 1
            If begins_open < 1 Then Exit Do
        End If
    Loop

    ' Count the remaining lines.
    CountTheRest fnum, num_lines, num_subs, num_functions, _
        num_properties, num_Comments

    ' Close the form file.
    Close fnum

    total_lines = total_lines + num_lines
    total_controls = total_controls + num_controls
    total_subs = total_subs + num_subs
    total_functions = total_functions + num_functions
    total_properties = total_properties + num_properties
    total_comments = total_comments + num_Comments
    ReportOnForm = txt & _
        "Lines:      " & Format$(num_lines, "@@@@@@@") & _
            vbCrLf & _
        "Controls:   " & Format$(num_controls, "@@@@@@@") & _
            vbCrLf & _
        "Subs:       " & Format$(num_subs, "@@@@@@@") & _
            vbCrLf & _
        "Functions:  " & Format$(num_functions, "@@@@@@@") _
            & vbCrLf & _
        "Properties: " & Format$(num_properties, "@@@@@@@") _
            & vbCrLf & _
        "Comments:   " & Format$(num_Comments, "@@@@@@@") & _
            vbCrLf
End Function
 
Function ReportOnModule and subroutine CountTheRest are similar to ReportOnForm so they are not shown here.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated