Reputation: 10087
I'm trying to find a way to count the total number of lines in all of the source files of a project I have. I've tried piping dir -r -name
into measure-object -line
, but that just counts the number of files I have.
Does anyone have a script to do this?
Upvotes: 55
Views: 38413
Reputation: 3356
Count line of code (exclude blank lines) of *.c
files recursively in a project folder
Get-ChildItem -Path \project-dir\src -Filter "*.c" -Recurse | ForEach-Object{(Select-String -Path $_ -Pattern .).Count} | Measure-Object -Sum
Count : 14
Average :
Sum : 979
Maximum :
Minimum :
StandardDeviation :
Property :
Upvotes: 0
Reputation: 10087
Thanks to everyone who answered. The way I ended up implementing this was
dir . -filter "*.cs" -Recurse -name | foreach{(GC $_).Count} | measure-object -sum
GC is alias for Get-Content
dir is alias for Get-ChildItem
Upvotes: 33
Reputation: 1
Count No. of lines
in a file inside a directory:
GCI . -Include *.* -Recurse | foreach{(GC $_).Count}
Count SUM of lines
in a file inside a directory:
GCI . -Include *.* -Recurse | foreach{(GC $_).Count} | measure-object -sum
Upvotes: -2
Reputation: 9
Since I needed something similar this is what I came up with.
one file type: GET-ChildItem -Recurse -Filter '*.cs' | Get-Content | Measure-Object -line
multiple file types: GET-ChildItem -Recurse -Include '.cs','.aspx', '*.ascx' | Get-Content | Measure-Object -line
Upvotes: 0
Reputation: 45858
Prints the file name and the line count:
Get-ChildItem -re -in "*.cs" |
Foreach-Object {
$fileStats = Get-Content $_.FullName | Measure-Object -line
$linesInFile = $fileStats.Lines
Write-Host "$_=$linesInFile"
}
Upvotes: 4
Reputation: 99
I just expiremented a bit and found that this command does indeed measure all c# files recursively:
Get-ChildItem -Filter *.cs -Recurse | Get-Content | Measure-Object -Word -Line -Character
Upvotes: 9
Reputation: 641
Get-ChildItem -Filter "*.cs" -Recurse | Get-Content | Measure-Object -line
Upvotes: 71
Reputation: 126702
dir **.txt -recurse | select Fullname,@{name="LineCount";expression={ @(get-content $_.fullname).count }}
Upvotes: 5
Reputation: 20179
Get-ChildItem . -Include *.txt -Recurse | foreach {(Get-Content $_).Count}
Condensed down a bit with aliases:
GCI . -Include *.txt -Recurse | foreach{(GC $_).Count}
Will give results similar to this:
Lines Words Characters Property
----- ----- ---------- --------
21
40
29
15
294
13
13
107
EDIT: Modified to recurse through subfolders.
EDIT 2: Removed use of Measure-Object.
Upvotes: 7