powered by myself

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:

for C#

^~(:Wh@//.+)~(:Wh@\{:Wh@)~(:Wh@\}:Wh@)~(:Wh@/#).+

fore 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).


Kick It on DotNetKicks.com

12 Comments to Line count in Visual Studio

  1. SirMike's Gravatar SirMike
    February 12, 2009 at 1:04 pm | Permalink

    OMFG :O What a monster ;)

  2. Yeorwned's Gravatar Yeorwned
    April 20, 2009 at 6:11 pm | Permalink

    Awesome! Thanks!

  3. Jakob's Gravatar Jakob
    May 7, 2009 at 7:20 pm | Permalink

    :D works perfect! Thanks a bunch…

  4. MatG's Gravatar MatG
    June 23, 2009 at 6:23 pm | Permalink

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

  5. Anonymous's Gravatar Anonymous
    July 23, 2009 at 12:56 am | Permalink

    Awesome. Love this.

  6. Steve's Gravatar Steve
    October 9, 2009 at 2:20 pm | Permalink

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

  7. Steve's Gravatar Steve
    October 9, 2009 at 2:21 pm | Permalink

    The above regex should work for VB.NET

  8. Germán Schuager's Gravatar Germán Schuager
    October 9, 2009 at 3:08 pm | Permalink

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

  9. chuck's Gravatar chuck
    October 22, 2009 at 2:12 am | Permalink

    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.

  10. ihsoft's Gravatar ihsoft
    November 23, 2009 at 10:41 pm | Permalink

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

  11. Anonymous's Gravatar Anonymous
    March 20, 2010 at 9:27 am | Permalink

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

  12. Anonymous's Gravatar Anonymous
    March 31, 2010 at 8:37 pm | Permalink

    nice

Keep in touch

Categories