views:

78

answers:

4

In a solution with lots of files and projects - how would you find all completely commented files? I assume that every line of code starts with // (EDIT: or is empty) in such files.

I am using VS 2008, C#, ReSharper is available.
I know, normally such files should not exist - that's what a source safe is for ...

A: 

Well, you could write a program (probably a console app) to recursively walk the directory and file tree. Read in all .cs files and check each line to see if its first non-space and non-tab characters are "//". If you wanted to get really fancy, you could count the total lines and the lines with "//" and display the percentages so you could catch files that didn't have absolutely every line commented out. You'll just need to understand a little bit about System.IO to get the files and string functions to look for the characters you are looking for. That should cover it.

Mallioch
I was hoping to get away without coding myself :/
tanascius
A: 

This should be close to what you're looking for: http://www.codeproject.com/KB/cs/csharplinecounter.aspx

Look for the method in the project that determines if a line is commented or not, and you can use that to build a count, etc.

routeNpingme
Nice idea, I gave it a try, but stopped now ... the code is very bad and somehow it did not count correctly - it could not find a commented file I prepared. I'll give it another try, later.
tanascius
+1  A: 

There is no way to achieve this with a simple search style with the components you've mentioned. Doing this would require a bit of interpretation on the file but could be done with a fairly simple script.

It sounds like you're looking for files without code though vs. files with all comments. For example if there are 1000 lines where 900 are commented and 100 are blank, it seems to meet your criteria.

The script should be fairly straight forward to write but you would need to look out for the following weird cases

  • Block comments
  • #if blocks which are always false. For example #if 0
  • Empty lines
JaredPar
You are right, I have to take care of blank lines, too. I was hoping to get a simple solution like the Find-Dialog :/ But there is non as it seems ...
tanascius
+1  A: 

To find all files in and under the current directory in which all lines begin with '//':

find . -type f -exec sh -c 'grep -vq "^//" {} || echo {}' \;

Note that this will report empty files.

The argument to grep can easily be expanded to account for whitespace, or generalized to match an arbitrary regex.

William Pursell
egrep -vq "(^//|^$)" did the job - thank you for this easy solution
tanascius