I have files indexed by the Windows Search service. I need function, which can find some string in text files. I have script in PowerShell, but it didn't work fine.
function search {
param($path, $word)
$c = $path + "\%"
$query = "SELECT
System.ItemName, System.ItemPathDisplay
FROM SystemIndex
WHERE System.ItemPathDisplay LIKE '$c' AND CONTAINS('$word')"
$ADOCommand = New-Object -ComObject ADODB.Command
$ADOConnection = New-Object -ComObject ADODB.Connection
$RecordSet = New-Object -ComObject ADODB.RecordSet
$ADOConnection.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
$RecordSet.Open($query, $ADOConnection)
try { $RecordSet.MoveFirst() }
catch [System.Exception] { "no records returned" }
while (-not($RecordSet.EOF)) {
if ($locatedFile) { Remove-Variable locatedFile }
$locatedFile = New-Object -TypeName PSObject
Add-Member -InputObject $locatedFile -MemberType NoteProperty -Name 'Name' -Value ($RecordSet.Fields.Item("System.ItemName")).Value
Add-Member -InputObject $locatedFile -MemberType NoteProperty -Name 'Path' -Value ($RecordSet.Fields.Item("System.ItemPathDisplay")).Value
$locatedFile
$RecordSet.MoveNext()
}
$RecordSet.Close()
$ADOConnection.Close()
$RecordSet = $null
$ADOConnection = $null
[gc]::Collect()
}
If $word = "Hello"
it works fine for files where we have
*some text * Hello * some text*
in the file, but not when we have Hello without spaces like:
HelloWorld
We can't also search when $word
is a phrase,for example "Hello World".
Anyone know how to fix it?