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

2 Answers 2

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 */.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
@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.
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.
@ashishg I can't figure out why you need select-string .??
I refered to the example given here -stackoverflow.com/questions/1244729/… .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.
|
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.