2016-02-20 92 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

相关问题