Counting number of lines in a solution

Tagged: Powershell Development

One question that I find myself asking frequently throughout the year is "how many lines of code have been written", or "how many lines of code is project/product X". Although line count really isn't a great indication of project complexity, size or development velocity, it is still sometimes useful to know and acts as a rough code for estimation models such as COCOMO.

I have been using Microsoft PowerShell since the beta stages, and although I'm not expert I definitely find the rich shell useful in certain situations.

This command will count the non-whitespace lines in C# source files, markup in ASPX/ASCX files and JavaScript; and exclude the generated designer files and JQuery.

PS C:\YourSolutionFolder> (dir -include *.cs,*.aspx,*.ascx,*.js -exclude *designer*,*jquery* -recurse | select-string .).Count

Sample Output:

675111

Hope that helps!

1 Comment

  • Mike said

    There's a simple app needed... can you write a simple C# parser that reads source files and counts the real number of lines... then returns the summary on a folder by folder basis.

    Of course you'll need to ignore comments and I'd argue ignore curly-brackets... I guess not all lines end in semi-colon (so is "for (...) {foo();}" one line or two?.. what about linq...) and ignore semicolons in strings...

    perhaps all lines aren't equal and returning count of the parsed elements would be much better... surely someone's tackled this already?!

    ok just count the non-space characters!

Add a Comment