Wednesday, January 7, 2009

Line count in Visual Studio

This is a nice trick that should be spreaded.

Select Edit -> Find & Replace -> Find in files... or just press CTRL+SHIFT+F

Check Use and select Regular expressions.

Type the following as the text to find:

C#
^~(:Wh@//.+)~(:Wh@\{:Wh@)~(:Wh@\}:Wh@)~(:Wh@/#).+
VB.NET (thanks Steve for the info!)
^~(:Wh@'.+)~(:Wh@/#).+

Select where you want to do the search/count: file, project or solution.

If you select Current project or Entire solution, you also need to specify the file types that will be included in the search.


Proceed with the "search" and at the bottom of the Find results window you will see the total line count.


The regular expression that is used match every line that are not a comment (//), a compiler directive (starts with #), a single opening or closing brace, or blank lines.

Note:
This post is based on this one from Philip Stears (the regex in the original post does not work correctly).

Submit this story to DotNetKicks

12 comments:

SirMike said...

OMFG :O What a monster ;)

Yeorwned said...

Awesome! Thanks!

Jakob said...

:D works perfect! Thanks a bunch...

MatG said...

That's a great tip - thanks! I've just reported our teams numbers back to our supervisor - maybe we'll get a break now..

Anonymous said...

Awesome. Love this.

Steve said...

^~(:Wh@'.+)~(:Wh@/#).+

Steve said...

The above regex should work for VB.NET

Germán Schuager said...

I've updated the post. Thanks for the info Steve.

chuck said...

Note that the term (:Wh@/#) above should probably be (:Wh@\#) for C++ at least. Exchange the right slash for the left slash (escape) character so that the compiler directives are skipped.

Also note that this regular expression will count multi-line comments without leading special characters as source code. In any case German's tip will give you a fast, rough estimate.

ihsoft said...

That's awesome! Easy and helpful, thank's a lot.

Anonymous said...

Hmm.. det rejser helt sikkert visse emner.
Bedste hilsner,
Nickie, frokostordning

Anonymous said...

nice