Counting Lines of Source Code
Jason Geffner (JasonGeffner) <jasongefmicrosoftcom> Monday, September 17 2007 20:51.00 CDT


Im reviewing the source code for a rather large project this week and I wanted to update my Facebook status by saying something like, "Jason is reviewing 100,000 lines of Java for security vulnerabilities." However, being the perfectionist that I am I wanted to give the real number of lines of code.


I wasnt aware of any built-in functionality in Visual Studio to do this, and after three minutes of Googling, I found a lot of Visual Studio plugins that could do this but unfortunately I didnt find any instructions on how to do this with just plain Visual Studio. And honestly, I didnt want to install a plugin (see http://blogs.msdn.com/oldnewthing/archive/2006/03/22/558007.aspx :)


I figured I could whip up a short C# program to do this, but even that seemed a little over-kill for such a simple task. Then I realized I could do this from a standard console window command prompt:


cmd /v:on
set lines = 0
for /r %a in (*.java) do (find /v /c "" "%a" > %temp% emp.txt
for /f "tokens=6" %b in (%temp% emp.txt) do (set /a lines += %b))
echo %lines%


The "tokens=6" part is specific to the source code directory structure for this particular project, and if any of the source code subdirectories contained spaces, youd have to tweak the code above a little. But hey, it worked out quite nicely, and it was a much cleaner solution than installing a plugin.


And Im sure theres an even shorter/simpler way to do this from a standard command prompt than with what I have above. Feel free to post cleaner "solutions" :)


(BTW, the actual number of lines turned out to be 348,523... that should keep me busy for a while.)



Comments
Posted: Wednesday, December 31 1969 18:00.00 CST