Question:
I have the following powershell script to count lines per file in a given directory:
1 2 |
dir -Include *.csv -Recurse | foreach{get-content $_ | measure-object -line} |
This is giving me the following output:
1 2 3 4 5 6 7 8 9 |
Lines Words Characters Property ----- ----- ---------- -------- 27 90 11 95 449 ... |
The counts-per-file is fine (I don’t require words, characters, or property), but I don’t know what filename the count is for.
The ideal output would be something like:
1 2 3 4 5 6 7 8 9 |
Filename Lines -------- ----- Filename1.txt 27 Filename1.txt 90 Filename1.txt 11 Filename1.txt 95 Filename1.txt 449 ... |
How do I add the filename to the output?
Answer:
try this:
1 2 3 4 5 6 7 8 |
dir -Include *.csv -Recurse | % { $_ | select name, @{n="lines";e={ get-content $_ | measure-object -line | select -expa lines } } } | ft -AutoSize |