Skip to content

Instantly share code, notes, and snippets.

@jeff-heienickle
Created March 8, 2019 15:17
Show Gist options
  • Save jeff-heienickle/0df007ac52b6f16fc1a0bc8df0542016 to your computer and use it in GitHub Desktop.
Save jeff-heienickle/0df007ac52b6f16fc1a0bc8df0542016 to your computer and use it in GitHub Desktop.
find count powershell
#Quick and dirty PS script for counting lines of code in a directory. Output is dumped to a simple CSV file.
#Note that this script doesn't count blank lines.
#Parameters:
# path - the path containing the code files (note that the script will recurse through subfolders
# outputFile - fully qualified path of the file to dump the CSV output
# include (Optional) - file mask(s) to include in the count (deafults to *.*)
# exclude (Optional) - file mask(s) to exclude in the count (defaults to none)
# Example (count lines in target path including *.cs but excluding *.designer.cs)
# .\find.ps1 -path "C:\Source" -outputFile "C:\Temp\out.csv" -include "*.js" -find "LocalStorage"
param([string]$path, [string]$outputFile, [string]$include = "*.*", [string]$exclude = "", [string]$find = "")
Clear-Host
Get-ChildItem -re -in $include -ex $exclude $path |
# Filter files according to a script.
Where-Object {
# Pick only the files that contain the string 'dummy'.
# Note: The -Quiet parameter tells Select-String to only return a Boolean. This is preferred if you just need to use Select-String as part of a filter, and don't need the output.
(Select-String -InputObject $_ -pattern $find -SimpleMatch -Quiet) -eq $true
} |
Foreach-Object { Write-Host "Counting $_.FullName"
$fileStats = Get-Content $_.FullName | Measure-Object -line
$linesInFile = $fileStats.Lines
"$_,$linesInFile" } | Out-File $outputFile -encoding "UTF8"
Write-Host "Complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment