Created
August 18, 2017 07:54
-
-
Save bayotop/bd36096cdc295c5c023eabf4d9cf9681 to your computer and use it in GitHub Desktop.
EOC - Post #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[Parameter(Mandatory=$true)] | |
[string] $Wordlist, | |
[array] $Exclude = @("*.jpg","*.png","*.dll","*.exe","*.so","*.o"), | |
[string] $OutputFile = "usages.csv" | |
) | |
function Prepare-OutputFile($OutputFile) { | |
if (!(Test-Path $OutputFile)) { | |
Out-File -FilePath $OutputFile | |
} | |
else { | |
Clear-Content $OutputFile | |
} | |
} | |
function Get-FileNamesFromFolder($Path, $Exclude) { | |
Write-Host "Looking for usages in $Path..." | |
return Get-ChildItem -Path $Path -Exclude $Exclude -Recurse -ErrorAction SilentlyContinue -File | % { $_.FullName } | |
} | |
function Find-Usages($Files, $Wordlist, $OutputFile) { | |
foreach ($word in Get-Content $Wordlist) { | |
# Too much false positives for words of length 2... | |
If ($word.Length -lt 3) { | |
$word = " $word " | |
} | |
foreach ($filename in $Files) { | |
Get-Content $filename | | |
Select-String -SimpleMatch $word | | |
Select-Object @{n='Pattern';e={$word}}, @{n='Path';e={$filename}}, LineNumber, Line | | |
Export-Csv $OutputFile -Append | |
} | |
} | |
Write-Host "Saved all usages to $OutputFile!" | |
} | |
$files = Get-FileNamesFromFolder -Path $Path -Exclude $Exclude | |
Prepare-OutputFile -OutputFile $OutputFile | |
Find-Usages -Files $files -Wordlist $Wordlist -OutputFile $OutputFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment