Log in

View Full Version : Counting Lines of Source Code


REBlog
October 19th, 2007, 20:27
I'm reviewing the source code for a rather large project this week and I wanted to update my Facebook status ("http://www.facebook.com/profile.php?id=416180") 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 wasn't 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 didn't find any instructions on how to do this with just plain Visual Studio. And honestly, I didn't 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 ("http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx") /vn
set ("http://technet2.microsoft.com/windowsserver2008/en/library/5fdd60d6-addf-4574-8c92-8aa53fa73d761033.mspx") lines = 0
for ("http://technet2.microsoft.com/windowsserver2008/en/library/e275726c-035f-4a74-8062-013c37f5ded11033.mspx") /r %a in (*.java) do (find ("http://technet2.microsoft.com/windowsserver2008/en/library/2ca66b22-3b7c-4166-8503-eb75fc53ab461033.mspx") /v /c "" "%a" > %temp%\temp.txt
for ("http://technet2.microsoft.com/windowsserver2008/en/library/e275726c-035f-4a74-8062-013c37f5ded11033.mspx") /f "tokens=6" %b in (%temp%\temp.txt) do (set ("http://technet2.microsoft.com/windowsserver2008/en/library/5fdd60d6-addf-4574-8c92-8aa53fa73d761033.mspx") /a lines += %b))
echo ("http://technet2.microsoft.com/windowsserver2008/en/library/fb9fcd0f-5e73-4504-aa95-78204e1a79d31033.mspx") %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, you'd 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 I'm sure there's 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.)

http://malwareanalysis.com/CommunityServer/aggbug.aspx?PostID=401

http://malwareanalysis.com/CommunityServer/blogs/geffner/archive/2007/09/17/401.aspx