Search Within Files

Powershell!

get-wordmatches.ps1
Code:
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$directory,
[Parameter(Mandatory = $false, Position = 1)]
[string]$wordFile = "C:\Scripts\files\words.txt"
)
$words = get-content $wordFile
$regex = "(?i)" + ($words -join "\b|") + "\b"
$matched = get-childitem $directory -filter *.txt | get-content | foreach {[regex]::matches($_, $regex)} | %{ $_.Groups[0].Value } | select -Unique
$not_matched = @()
foreach($word in $words)
{
    if($matched -notcontains $word)
    {   $not_matched += $word }
}

Write-Host "`nSearching all text files found in '$directory' for the words:"
Write-Host "`n$words" -fore cyan

Write-Host "`n`nWords Found" -fore green
Write-Host $matched 

Write-Host "`nWords NOT Found" -fore red
Write-Host $not_matched
 
I assume you have used PowerShell before. If not let me know and I will write the couple of steps you need to do. I would link to a post where ive explained it before, but can't find any! :(
 
I had to set the security to Unrestricted (found this out with a bit of Googling) and I changed it to search in *.xml files. It is running now and taking up 50% of CPU... been like that for a few minutes now, so lets see what happens :)
 
Notepad++ almost does this....

Mine exactly does this! :p

I had to set the security to Unrestricted (found this out with a bit of Googling) and I changed it to search in *.xml files. It is running now and taking up 50% of CPU... been like that for a few minutes now, so lets see what happens :)

If you want to improve the security of your computer slightly, you can set

Code:
Set-ExecutionPolicy RemoteSigned
Which only lets scripts created locally run unless it has been digitally signed.

50% CPU :eek: How many files did it have to go through?

SiriusB... I love you! :D

:D
 
There were about 500 files... and after 5 minutes of processing it was done. I only have a 3GHz Pentium 4 CPU at work, so it would have been a lot faster if it was any good. I also set it to output the results to a text file.

You have saved me about 2 days of work, because I do not have to run a search for each of the 200 words manually :D
 
Back
Top Bottom