3

I am pretty new to powershell.

Need help in writing a script to count the total number of lines in a visual studio project provided I ommit (ignore) the commented lines in the code. Eg: <'> single quote as in vb.net. Whichever line is commented i.e begins with <'> single quote I do not need to consider in the lines count of the file.

I've been successful so far in writing the script for counting the number of lines in a project based on the file types(say *.vb etc). like below

(dir -include *.cs,*.xaml -recurse | select-string .).Count
  • I now need how can I ignore the line beginning with a single quote while counting?
  • Could you suggest something i can include alongwith the above code line??

Any help would be greatly appreciated!

Thanks, Ashish

ashish g
  • 281
  • 3
  • 7
  • 14

2 Answers2

6

Try

(gc c:\file.vb | ? { !$_.startswith("'") }).count

Edit after comment:

try this:

dir c:\myfolder -include *.cs,*.xaml,*.txt -Recurse | % { $count = (gc $_ |  ? { $_ -notmatch '^\s*$|^''|/\*|\*/' }).count; if ($count) {write-host "$_ `tcount: $count"} }

this one count no empty lines, no line starting with ' and no lines containing /* or */.

CB.
  • 58,865
  • 9
  • 159
  • 159
  • Thanks for your response. But already have code for counting no of lines in a file like above. Could you suggest something i could tag along the existing code line. – ashish g Jul 20 '12 at 06:01
  • @ashishg Can you reformulate the question? In your OP you are asking for counting lines exclude lines starting with ' and all the answers provided are doing this job. – CB. Jul 20 '12 at 06:45
  • The idea was to merge your code with mine (as in OP). Can i do something like - (gc $filetypes -recurse | ? { !$_.startswith("'") } select-string .).Count? - (Here $filetypes=".vb",".asmx"..etc) I have files in multiple folders to be checked. Also I would like to check for multi-identifiers (operators) like ',/*,*/,blank line,space etc. Do not include in the count if the line contains any of these identifiers. Thanks for your time. – ashish g Jul 20 '12 at 10:37
  • @ashishg I can't figure out why you need `select-string .`?? – CB. Jul 20 '12 at 10:48
  • I refered to the example given here -http://stackoverflow.com/questions/1244729/how-do-you-count-the-lines-of-code-in-a-visual-studio-solution .Actually the script given by you works, the only thing I want is rather than checking for filename mentioned after gc, I want it to check the filetypes and I want it to check all file extensions in multiple folders. But I am not sure if I can use -recurse here with gc. Also it would be great if you could help me on how to test for multiple operators. – ashish g Jul 20 '12 at 11:18
  • Great!!!Thanks a lot Christian. That worked:) Just one last thing - what if I want to display the sum of all the number of lines of all the files. Eg: right now if I have 2 vb files (vb1.vb & vb2.vb) in project1, this script gives me the no of lines in each file. Like o/p - project1: 13 15.. what if I want it to display the sume i.e, as 28? Thanks! – ashish g Jul 23 '12 at 08:06
  • `(dir c:\myfolder -include *.VB -Recurse | % { (gc $_ | ? { $_ -notmatch '^\s*$|^''|/\*|\*/' }).coUNT} | Measure-Obje ct -Sum).sum` work on this... – CB. Jul 23 '12 at 08:16
  • Thanks a ton christian.. this works! Was having a tough time trying this:) Thanks again. – ashish g Jul 23 '12 at 11:03
  • Is it possible to sort by the count in descending order? – liang Dec 29 '14 at 09:49
1

Get all lines that do not start with a single quote, even if there's a leading space or tab in front of it. Pipe the result to the Measure-Object to count lines.

Get-Content file.ext | Where-Object {$_ -notmatch "(\s?)+'"} | Measure-Object
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thanks for your response. But already have code for counting no of lines in a file like above. Could you suggest something i could tag along the existing code line for my requirement. – ashish g Jul 20 '12 at 06:02