Counting words with Powershell
I have a hierarchy of note directories containing text files and I want to know how many words I wrote. I just put together a command to count words with Powershell. Here's what it looks like:
$i=0; foreach ($item in gci E:\path\to\notes -Name '*.txt' -Recurse) { $i = $i + [int]$(cat E:\path\to\notes\\"$item" | Measure-Object -word).Words }; echo $i
I'm happy with how easy this was to put together. I think I can even improve on this a little:
gci E:\path\to\notes -Name '*.txt' -Recurse | ForEach-Object -Process { gc E:\path\to\notes\"$_" | Measure-Object -word } | Measure-Object
-Property Words -Sum | select Sum
This avoids the explicit loop entirely in favor of pipes. Also we can use Measure-Object for summing instead of manually summing.