[英]Powershell Select-String and then obtain data on returned files
I admit I am quite tired today but even that is no excuse for the nightmare i seem to be creating for myself with Powershell at present. 我承认我今天已经很累了,但是即使那样,这也不是我现在用Powershell为自己创造的噩梦的借口。
Basically the aim is to search a directory for a string that is contained in some word documents. 基本上,目的是在目录中搜索某些Word文档中包含的字符串。 Then I need to return the file name, created date and the last write time.
然后,我需要返回文件名,创建日期和上次写入时间。 Easy I thought but at some stage I have gone well off track and am still climbing.
我以为很容易,但是在某个阶段我走得很好,仍然在攀登。
What I have done so far is run the search and export the path variable to a text file giving me the path to the files that contain the string. 到目前为止,我所做的是运行搜索并将path变量导出到文本文件,从而为我提供了包含字符串的文件的路径。 I have then tried to use a foreach loop to load the contents of the file and run a Get-ChildItem against each entry piped to a Select-Object Name,CreationTime,LastWriteTime.
然后,我尝试使用foreach循环加载文件的内容,并对通过管道传递给选择对象名称,CreationTime,LastWriteTime的每个条目运行Get-ChildItem。 I have finally > this to a text file.
我终于>将此文本文件。 However it now seems to be returing the info for every file in teh directory and not just those that contain the string I am searching for.
但是,现在似乎正在重新获取目录中每个文件的信息,而不仅仅是包含我要搜索的字符串的文件。 I get the feeling I am vastly over complicating this as i tend to with these things.
我感觉到我为使这些事情变得更加复杂而感到极大的困惑。 Any help greatly appreciated.
任何帮助,不胜感激。
Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba" | select-object path > C:\TRCALM\shibapath.txt
$files = get-content C:\TRCALM\shibapath.txt
foreach($i in $files){gci $i | select-object Name,CreationTime,LastWriteTime > C:\TRCALM\SHIBADates.txt}
You were almost there: 您几乎在那里:
$files = (Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba"| select-object path )
$k = foreach($i in $files){ (gci $i.Path | select-object Name,CreationTime,LastWriteTime) }
$k > C:\pst\SHIBADates.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.