2017-05-22 8 views
0

おはようございます、私はpowershellを初めて使っています。powershellを使用して特定のフレーズをフォルダから見つける方法

私はいくつかのフォルダから.doc、.docxで特定のフレーズを検索する方法を知りましたが、私が探しているものを正確に見つけることができません。例えば

、私はこのコード

Get-ChildItem 'C:\Users\koshiasu\Desktop\adhocs' -Filter *.sql |Select-String -Pattern "children" 

を実行したときには、この私のPowerShellの底に

結果1

Desktop\adhocs\18722 Parents.sql:11:      AND EXISTS (SELECT c.id_number FROM children c 
Desktop\adhocs\18722 Parents.sql:38:      AND EXISTS (SELECT c.id_number FROM children c 
Desktop\adhocs\2969 ADHOC - Parents in Dallas.sql:11:      AND EXISTS (SELECT c.id_number FROM children c 
Desktop\adhocs\2969 ADHOC - Parents in Dallas.sql:92:      AND EXISTS (SELECT c.id_number FROM children c 

のように私は.DOCのために同じことをしたいと思い示し、.docx

だから私はこのようなコードを変更しました

Get-ChildItem 'C:\Users\koshiasu\Desktop\ADHOCS_WORD' -Filter *.doc, *.docx |Select-String -Pattern "Allocations" 

が、エラーが、私は結果1のように表示するようにコードを変更する方法

Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported. 
At line:2 char:58 
+ Get-ChildItem 'C:\Users\koshiasu\Desktop\ADHOCS_WORD' -Filter <<<< *.doc, *.docx |Select-String -Pattern "Allocations" 
    + CategoryInfo   : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException 
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand 

ある

はどうもありがとうございます

私は基本的に、転がっ同じ片付けた何かのためのコードを持っていた
+2

'Get-ChildItem -Filter'は1つのフィルタパターンしか受け付けません。代わりに' Get-ChildItem | Where-Object {'.doc'、 '。docx' -contains $ _。Extension} | Select-String ... ' 。それはおそらく動作しません。 '.docx'ファイルは基本的にXML文書のzipフォルダですが、正規表現は使用しません –

答えて

1

あなたが探しているもののアイデアを得ることができるように、わずかに上がってください:

$Path = "C:\Test\" 
$Find = "Allocations" 
$WordExts = '.docx','.doc','.docm' 

$Word = New-Object -ComObject Word.Application #create word obj 
$Word.Visible = $false #hide the window 

$ValidDocs = Get-ChildItem $Path | ? {$_.Extension -in $WordExts} | ForEach { #Foreach doc/docx/docm file in the above folder 
    $Doc = $Word.Documents.Open($_.FullName) #Open the document in the word object 
    $Content = $Doc.Content #get the 'content' object from the document 
    $Content.MoveStart() | Out-Null #ensure we're searching from the beginning of the doc 
           #term,case sensitive,whole word,wildcard,soundslike,synonyms,direction,wrappingmode 
    if ($Content.Find.Execute($Find,$false,  $true,  $false, $false, $false, $true, 1)){ #execute a search 
     Write-Host "$($_.Name) contains $($findText)" -ForegroundColor Green 
     $_.FullName #store this in $ValidDocs 
    } else { 
     Write-Host "$($_.Name) does not contain $($findText)" -ForegroundColor Red 
    } 
    $Doc.Close() #close the individual document 
    $Doc = $null #null it just in case 
} 

$Word.Quit() #quit the word process 
$Word = $null #null it just in case 

return $ValidDocs #return list of docs with the word in them 
関連する問題