[英]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.