2015-10-13 50 views
-1

我有一個包含關鍵字列表的平面文件。我可以格式化這個文件,但它是需要的。從列表中搜索關鍵字(在文件中)並返回完整路徑

我想搜索服務器上的關鍵字,我希望腳本將包含該關鍵字的所有路徑返回到文件。完整路徑+文件名會更好。

我需要在滿載服務器上查找圖像,每次搜索需要30-60秒。我們需要找到約1000個關鍵字。因此,如果我能得到一個能搜索所有內容並將其報告給excel文件的腳本,那麼這將節省時間。

Product1 c:\products\collection1\season2\product1.psd 
Product1 c:\products\collection2\season3\product1-back.psd 
Product1 c:\products\collection1\rejected\front-product1.txt 
Product2 c:\products\collection3\season1\product2-back.pdf 
Product3 c:\products\collection2\season4\product3-new.tif 

P.S.:我的平面文件所需的輸出文件的

Product1 
Product2 
Product3 

實施例的

實施例返回的路徑將是UNC路徑,如\\server\products\collection

+0

爲什麼你這是完全不同的兩種東西'powershell'和'批量file'標籤(儘管你可以嵌入雙方)? – wOxxOm

+0

我添加了PowerShell和批處理文件,因爲我不關心最終結果的方法。 – user5442433

回答

0

然後,它完全寫出來,讓對象跟蹤的結果:

$searchWords = gc c:\filename.txt 
$results = @() 
Foreach ($sw in $searchWords) 
{ 
    $files = gci -path C:\products -filter "*$sw*" -recurse | select FullName 

    foreach ($file in $files) 
    { 
     $object = New-Object System.Object 
     $object | Add-Member -Type NoteProperty –Name SearchWord –Value $sw 
     $object | Add-Member -Type NoteProperty –Name FoundFile –Value $file 
     $results += $object 
    } 

} 

$results | Export-Csv result.csv -Delimiter "`t" -NoTypeInformation 
+0

非常好!你節省了我的一天 – user5442433

0

這是什麼意思?

gc filename.txt | % {gci -path c:\products -filter "*$_*" -recurse} | select FullName | Export-Csv result.csv -Delimiter "`t" 
+0

它的超棒!完全符合我的意圖。唯一能讓它更有效的是,如果我可以在路徑之前輸出文件中的關鍵字名稱,例如: product1,路徑 – user5442433