简体   繁体   English

如何在特定日期最后更改的文件中查找字符串?

[英]How to find a String in a file which was lastly changed on a specific date?

The following piece of code works, but only for today's date.以下代码有效,但仅适用于今天的日期。 What do I have to change to put in a specific date, eg the 3rd of May 2022?我必须更改什么才能输入特定日期,例如 2022 年 5 月 3 日? I also want to do that with a Read-Host .我也想用Read-Host做到这一点。

Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse | 
    Where-Object LastWriteTime -ge ([datetime]::Today) | 
    Sort-Object LastWriteTime | 
    Select-String -Pattern Test
(get-item "C:\tmp\test.xml").lastwritetime.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

The datatype is DateTime, so no need to build matching strings.数据类型为 DateTime,因此无需构建匹配字符串。

In regards to the requirement to use read-host you could do:关于使用 read-host 的要求,您可以这样做:

[datetime]$DateTime = read-host

By doing so you cast the Input as DateTime but this requires that you enter a supported format.通过这样做,您将 Input 转换为 DateTime,但这需要您输入支持的格式。 I think its easier if you have to specify the days back to calculate the DateTime information, eg;我认为如果您必须指定日期来计算 DateTime 信息,它会更容易,例如;

[int]$daysBack = read-host
Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse | 
    Where-Object LastWriteTime -ge (get-date).AddDays(-$daysback) | 
    Sort-Object LastWriteTime | 
    Select-String -Pattern Test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 windows 服务器 2012 的日期范围之间查找特定文件, - How to find a specific file between a date range in windows server 2012, 如何制作一个bat文件来查找文件路径并将特定文件复制到其他位置 - How to make a bat file which find a file path and copy a specific file to an other location 如何删除文件名中包含特定字符串的文件夹和所有子文件夹中的文件? - How to delete files in folders and all subfolders which contain specific string in file name? 如何在 Windows 上的大型文本文件中搜索和提取包含特定字符串的行? - How can I search and extract lines which contain a specific string in a large text file on Windows? 如何在字符串中查找字符的特定实例 - How to find specific instance of character in string 如何查找文件中的任何一个单词是否在一个很长的字符串中,该字符串在 CMD 批处理中经常更改 - How do I find if any one of the words in a file is in a very long string, which changes frequently in CMD batch 如何解析由 CRT 函数形成的日期字符串? - How to parse date string which is formed by the CRT function? 如何在小文本文件中检查特定字符串 - How to check for a specific string in a small text file 批处理文件,在文本文件的一行中找到字符串“ -date = 8/27/2015”,并将该行写到另一个文件中 - Batch file to find string “-date=8/27/2015” in a line of a text file and write the line out to another file 如何将输出重定向到名称是当前日期和时间的文件? - How to redirect output to a file which name is the current date and time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM