views:

637

answers:

3

I am currently researching a solution for counting lines of code in C#.

I pretty much need a combination of the following two tools:
http://richnewman.wordpress.com/2007/07/01/c-and-vbnet-line-count-utility/
http://www.locmetrics.com/index.html

My problem is that I need to recursively scan a folder containing a lot of visual studio solutions. So can't really use the first tool without any major work on its code, as it's only able to scan a single solution at a time.
But I also need to split the results for each solution, preferably even the contained projects. This disqualifies the second tool I found. I also found NDepend which suffers from the same problem.

Do you know of any free tools that do what I need? I am unable to find anything suitable.

+3  A: 

NDepend is a great tool designed for measuring and visualising code metrics and complexity.

Powershell would do it:

(dir -Include *.cs -Recurse | select-string .).Count

Counting Lines of Source Code in PowerShell:

Line count per path:

   gci . *.cs -Recurse | select-string . | Group Path

Min / Max / Averages:

   gci . *.cs -Recurse | select-string . | Group Filename | Measure-Object Count -Min -Max -Average

Comment ratio:

   $items = gci . *.cs -rec; ($items | select-string "//").Count / ($items | select-string .).Count


## Count the number of lines in all C# files in (and below) 
## the current directory. 

function CountLines($directory) 
{ 
    $pattern = "*.cs" 
    $directories = [System.IO.Directory]::GetDirectories($directory) 
    $files = [System.IO.Directory]::GetFiles($directory, $pattern) 

    $lineCount = 0 

    foreach($file in $files) 
    { 
        $lineCount += [System.IO.File]::ReadAllText($file).Split("`n").Count 
    } 

    foreach($subdirectory in $directories) 
    { 
        $lineCount += CountLines $subdirectory 
    } 

    $lineCount 
} 

CountLines (Get-Location) 

Also, Line Counter

Mitch Wheat
Unfortunatly this counts all text lines, I am more interested in real code lines, excluding comments for example and maybe even auto-generated code.
Eric
@Eric - your productivity is going to go way down if you don't count comments and auto-generated code. :-)
tvanfosson
@Tvanfosson I know ;-)
Eric
Couldn't you use a combination of this method to find all the .sln files, then feed each of them into the first tool you mentioned?
Niall C.
@nc97217 sure I could come up with my own implementation, just hoped this has already been done
Eric
As far as I can tell NDepend also only works on a single solution/assembly at a time. But I need to scan the complete repository at once.
Eric
@Eric: that's true. But it does have many useful metrics; much more useful than simple line counts
Mitch Wheat
+1  A: 

What you need is logical lines of code counting as defined here: How do you count your number of Lines Of Code (LOC)

If you use NDepend to count your number of lines of code you can still append all your VS sln in a NDepend project. However logical lines of code is a metric inferred from PDB files so make sure that all your assemblies have corresponding PDB files associated.

Also you might be interested by:Why is it useful to count the number of Lines Of Code (LOC) ?

Patrick Smacchia - NDepend dev
A: 

In the end I went with LocMetrics, unfortunately this didn't really solve my per-solution problem.

But the folder structure of the reposiotry maps well enough to solutions so I was decided to use the tool above.

Thanks everybody for helping

Eric
Why did you down vote this answer? Should I have done something differently?
Eric
this program does not calculate the lines of code correctly
Rookian