How do you count the lines of code your application has? As far as I know Visual Studio does not provide this functionality, so needless to say, we are doing that with PowerShell 😉
$num = 0
dir c:\root\ -include *.cs -Recurse | ForEach-Object {
$file=[array](Get-Content $_ )
$num += $file.length
}
$num
You obviously need to change c:\root
to your root folder path, and change the *.cs
mask to the one applicable to your source code.
Tags: PowerShell, software, software development
Another approach would be:
dir c:\root\ -include *.cs -Recurse | Get-Content | Measure -Line
Make that Measure-Object (measure is an alias I set up).
By the way, “-filter *.cs” is a lot faster than “-include *.cs” … personally I like to not count empty lines, so I filter them out with something like:
ls -recurse -filter *.cs | gc | ? { -not $_.Trim().Length -eq 0)) } | measure-object -line
If you don’t want to count the comment lines, you could also filter those out (note that you have to change your comment string if you’re not working on C#, and that this doesn’t exclude /* comment blocks */)
ls -recurse -filter *.cs | gc | ? { $str = $_.Trim(); -not ($str.StartsWith(“//”) -or ($str.Length -eq 0)) } | measure -line
Joel, measure-object -line will skip empty lines by default. But for other commands you can filter empty lines just this way: |?{$_}
Wow, I didn’t know about Measure-Object – it turns the script into a real one-liner – which is always the goal here, right? 😉
Learning something new every day! Thanks guys!
It was first script in Monad shell I’ve witten myself. Thank you for reminding 🙂
thats very useful , i will use it
thanks