如何使用PowerShell脚本在文件中搜索单词 [英] How to search a word in a file using PowerShell script

查看:176
本文介绍了如何使用PowerShell脚本在文件中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有350个文件夹的列表,每个文件夹都有一个文件访问日志.我需要在所有350个文件夹下的所有350个文件中搜索名称"Hound",并在其访问日志文件中显示包含名称"Hound"的文件夹的名称. 以下是我的代码,有人可以帮助我,在此处添加一些内容以获得所需的输出吗?

I have a list of 350 folders and each folder has a file Access log. I need to search all 350 files under all 350 folders for a name "Hound" and display the name of the folders which contain the name "Hound" in their access log file. Below is my code, can someone help me with what should be added here to get the desired output, please?

   #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
   }

推荐答案

这是一种相当基本的方法:

Here's a fairly basic way to do this:

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

如果需要确保仅搜索名为access.log的文件,请指定过滤器:

If you need to make sure that only files called access.log are searched then specify a filter:

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

这篇关于如何使用PowerShell脚本在文件中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆