Introduction

While updating my cv, I wanted a quick way of seeing how many lines of code my application contained (160,430 lines!). I haven't looked into Visual Studio Add-ins too much so I wrote a simple stand-alone application that has a few nice features.

Using the Application

Fire up the application and select a folder which contains your code or projects - that's it. The application then searches for all code files and counts every line of code. It displays the files included along the line count. It then generates a total at the bottom.

Checking the "Include subfolders" will instruct the software to search all the sub folders, and you also have the option to look for particular source files, *.cs for C# and *.vb for VB.net.

Nice Feature

Some of my projects are spread across two folders, (client / server) so I needed a way of adding these two totals together. After calculating the total for one folder, you can click on the Grouping > Add menu to add the total and directory to a list. As you tally up the folders a total is show in the menu.

Not a lot of Code

There isn't a lot of code to this but here's the important parts.

Using the DirectoryInfo.GetFiles function allows for easy use of searching just a folder or all folders contained within it with a search pattern, *.cs or *.vb. It's fast and saves time having to write your own recursive function.

Counting the lines

I figured that to count the lines I should open the text and use a RegEx "\n" to calculate the lines, but I guess that the RegEx is doing the same thing I would which is open the file and read each line. So I just use a loop to count the lines, which is just as quick I reckon.

Collection.RemoveAt

Got stuck trying to remove items within a collection because I only need to remove a certain amount of items from a particular index. Items 0 to 3 contain the Add, Clear All, Total and a Separator. This solution below sorts this out.

// if there was two items or more in the collection then
// one item would always be left.
for (int i = 4; i < toolStripGrouping.DropDownItems.Count; i++)
{
toolStripGrouping.DropDownItems.RemoveAt(i);
}
// this fixes it
int count = toolStripGrouping.DropDownItems.Count;
for (int i = 4; i < count; i++)
{
toolStripGrouping.DropDownItems.RemoveAt(4);
}

Line Counter Code

The updated line counter code was changed to include or exclude certain lines. The easiest way for me to do this was to first remove the "\t" tabs from the line and then investigate the line of code.

History of Searches

I thought that re-browsing projects I have already counted was getting a bit repetitive so adding a history made sense.

Basically it works like this: if a search is performed it checks to see if it exists in the history stored in the ToolStripItems. If it does then it just moves that ToolStripItem to the top using the Insert function with a index set to 0. If it doesn't then it first checks to see if it has reached the maximum amount allowed (currently 5), removing the last one if it has and inserting the new search into index.

Conclusion

In my opinion, a neat little application that does exactly what it says on the tin. It's helped with my cv but no job offers just yet!

History

Added the ability to not count blank lines, comments, Visual Studio auto-generated code, or designer files. This is accessed through the File Options menu items.

Changed the search routine so that you can browse multiple extensions (*.cs|*.vb) at the same time.

Copy the current list of files and number of lines to the clipboard for email/printing

Redesigned the Browse feature with a Toolbar

Added a Recount button

A quick history list of the last 5 (configurable in code) searches - explained below.

Add the additional code to filter vb files.

If someone could send me the "#region" and "#endregion" in C++.net I can add this to the routine as well.

No ASPX just yet. I have a real software job to do.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Unfortunately in the line cell you will have a single cell containing the number with the lines text e.g "1000 lines"

To remove the text insert the excel formula <b>=LEFT(C7,LEN(C7)-6) </b> where C7 is a line number cell.

Copy the new column that contains just the numbers.Then paste using special values only into a new column. Do this by right clicking on and empty cell in an empty column and selecting paste special->values.

Removing directory from the file string:Add just the to be removed directory string in a cell (say A6)=SUBSTITUTE(A7,A$6,"",1)

I have been running your .exe because I only have VS2003 on this machine but I just took a look at the code and have the following bug that will skew the line counts up.

Form1.cs::CountFiles::Line 137The s = s.Replace() might be safer to be replaced with s.Trim() to also handle space characters instead of only tabs (some editors use spaces instead of tabs, actually some users do to! ).

Nice app and I really appreciate the grouping feature you added so many thanks!

Small things for us poor (and by definition stupid ) VB coders ... it's nice that we have the *.vb filter for the file find, but the actual execution after the folder selection does not use the VB syntax for comments ( which is ' you C# snobs ) and we also have quotes in our #Region definitions.

Perhaps a config file that defines these symbols and that would then also allow any user to add additional languages...!?

Other passing thoughts:- a refresh button to invoke the counting (for when you change counting options and saves the opening of the folder dialog)- ability to output the group info to file or clipboard (so that I can email the current counts without typo)- and then also output the current files and line counts (i dont have a reason at the moment)

Anyway, thanks for this mate - I have already used it to count current project and some interesting results!!!

Thanks for your comments, the sort feature is a real pain using the ListView, because I would have to write my own Comparer to make it work, but it's something that needs doing. Maybe it I stored the data in a DataTable first and then put it in the listview, I could sort the information before I enter it.

The Count of comments and other inclusions / exclusions is something I will add tomorrow or the day after.