Source Line of Code Counter

Posted on December 26, 2007
Filed Under C# | 9 Comments     

Source Line of Code Counter
.NET Framework

Since I couldn't find a SLOC (source line of code) counter that satisfies me, I decided to write one. This SLOC counter equipped with a file system browser which is very handy for precisely defining inclusion and exclusion of files/directories. Besides that, the SLOC counter supports pattern matching for file inclusion and exclusion, and directory inclusion using regular expression. The usage section of this post has a detailed list on how to use pattern match.

This LOC counter is written in Visual C# 2005. That means you need Microsoft .NET Framework 2.0 to run this program. You can download a free copy of .NET Framework from here.



  • File/Directory browser for selecting multiple project location
  • Regular expression pattern match
  • Counting results can be sorted by file name or line count
  • Export counting results to file
  • Custom counting rules can be defined

Basic Usage

Select a project directory and press the Start button to start counting.

You can select a counting rule to refine the counting result, e.g., to strip out comments for designated languages.

Advanced Usage

In SLOC counter, one can define patterns through regular expression to filter unwanted files. To define multiple patterns, use comma to separate each pattern. Below are examples on how to use patterns.

If you are new to regular express, this site has some excellent tutorials to get you started.

Include only files with extension .php, .html or .css:


Exclude subversion directory:


Include files which file names contain "config" and not under directories which names contain ".svn":


Custom Counting Rules

Counting rule define how lines in a file are counted. Each counting rule is defined by a regular expression. In the event that the line in the file which is being counted is matched, this line will be excluded from counting. Typical scenario for using counting rule is to exclude comments.

There are two types of counting rules: line and page, stored in <name>.line.conf and <name>.page.conf respectively, where <name> is the name of the counting rule.

In line counting rule, each line represents a counting rule in the form of regular expression. If the line in the source file matches any of the rules defined in <name>.line.conf, the this source code line will be ignored from counting.

Page counting rules are defined in a similar way except they are paired. In .page.conf, starts from first line, every two lines define a page counting rule. The first line of each two lines signifies beginning pattern, the second line signifies ending pattern.

Here's counting rules for PHP language are defined:

In php.line.conf:

^ *<\?php *$
^ *\/\/.*
^ *$
^ *\?> *$


^ *\/\*
\*\/ *$

Rules in php.line.conf tells the program to ignores PHP tags <php and ?>, and single line comment //.

Rules in php.page.conf tells the program to ignores multiline comment which denoted by /* */.

Creating Your Own Counting Rule

Create a counting rule in addition to bundled ones is easy. All you need to do is create these two files under the directory where you launch your application:


where is the name of counting rule.

Related Links

If you find this post useful, consider donating a few bucks to help the server running cost.

Go to Top

Subscribe to keep track of your comment.
9 Comments so far
  1. dev waring January 28, 2008 8:22 pm
    is it possible to let the single multiline command in php to be put automaticaly for each page?Thanks
  2. Cuong January 29, 2008 11:36 am

    Sry, I am not following you. Can you show me a detailed example of what you need?
  3. Sarika February 12, 2008 7:45 pm

    I have downloaded the YUI toolkit. I am trying to select multiple files and upload them all at once.

    I would greatly appreciate any help from you, if you could send me a detailed example of how its implemented. I am trying to implement like flicker.

  4. Sarika February 12, 2008 7:49 pm
    Incase you have an example, you could mail it to me @ sakvk@yahoo.com. I am trying to implement it in JSP,javascript.

    I would gr8ly appreciate any help from you.

  5. Rod July 15, 2008 12:59 pm
    Great tool! it is very useful. Thanks..
  6. tvdias December 8, 2008 6:47 pm
    awesome!!! very very very good tool! thanks a lot!
  7. Alessandro Febretti April 20, 2009 6:57 pm
    Great app, this is exactly what I needed :)
  8. Levi May 10, 2009 9:15 pm
    The default rules are no good. The PHP rules only ignore spaces, not tabs. Replace all " *" with "\s*" in the rules to make them work with a file that has tabs and/or spaces in it. of course you'll need to add more rules if you put { or } on their own lines, or use anything other than // for comments.

  9. Mauro Escobar May 25, 2009 10:40 am
    Great tool, thanks!
    That's a very useful tool, but I noticed a few messages like the one below:

    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    I don't know why does it happen. But even with that little problem I could do my job. Thanks.
Code Central Special


  • Tell other about your screen resolution (pulled from web stats):

    View Results

    Loading ... Loading ...