2016-02-20 171 views
1

我有一個350個文件夾的列表,每個文件夾都有一個文件訪問日誌。我需要在全部350個文件夾下搜索名爲「Hound」的所有350個文件,並在其訪問日誌文件中顯示包含名稱「Hound」的文件夾的名稱。 下面是我的代碼,有人可以幫我在這裏添加什麼來獲得所需的輸出嗎?如何使用PowerShell腳本搜索文件中的單詞

#List all the folders in C:\testfolder 
    $folders = (Get-ChildItem -Path "C:\testfolder" | Where-Object{$_.Attributes -eq "Directory"} | Select Fullname) 

    #looping all folders 
    Foreach ($folder in $folders) 
    { 

    #Here I need to look for the word "Hound" inside the Access.log file and if the word is there, it should display the name of the $folder which has the word 
    } 

回答

1

這裏是做這一個相當基本的方法:

Get-ChildItem -Path d:\testfolder -Recurse | Select-String -Pattern "Hound" 

如果您需要確保只有被稱爲access.log文件進行搜索,然後指定一個過濾器:

Get-ChildItem -Path d:\testfolder -Include "access.log" -Recurse | Select-String -Pattern "Hound" 
+0

感謝千電子伏,這對我有用:) – CodeX

+0

@CodeX =你知道你可以標記解決方案是正確的(請參閱此答案左側的勾號按鈕),然後upvote呢?這就是我們的網站的工作原理,如果人們看到將答案標記爲已接受的記錄,那麼他們更有可能爲您提供幫助。 – Kev

+0

我做到了,但是一旦我獲得了15個聲望,那麼只有我公開標記答案。 – CodeX

相關問題