我有一个具体日期"2021/11/28",我希望该文件列表,从实例的文件名(见下文),其文件的名字是大于2021/11/28. 记得不创立时文件名称。
"test_20211122_aba.*"
"abc_20211129_efg.*"
"hij_20211112_lmn.*"
"opq_20211130_rst.*"
我期望得到的
"abc_20211129_efg.*"
"opq_20211130_rst.*"
真的很感激你的帮助。
我有一个具体日期"2021/11/28",我希望该文件列表,从实例的文件名(见下文),其文件的名字是大于2021/11/28. 记得不创立时文件名称。
"test_20211122_aba.*"
"abc_20211129_efg.*"
"hij_20211112_lmn.*"
"opq_20211130_rst.*"
我期望得到的
"abc_20211129_efg.*"
"opq_20211130_rst.*"
真的很感激你的帮助。
你不严格地需要分析你的串到的日期([datetime]
实例):因为该日期 串 镶嵌在你的文件名称均在一种格式,其中他们的 词汇 的排序等同于 按时间顺序 排序,可以比较的串直接陈述:
# Simulate output from a Get-ChildItem call.
$files = [System.IO.FileInfo[]] (
"test_20211122_aba1.txt",
"abc_20211129_efg2.txt",
"hij_20211112_lmn3.txt",
"hij_20211112_lmn4.txt",
"opq_20211130_rst5.txt"
)
# Filter the array of files.
$resultFiles =
$files | Where-Object {
$_.Name -match '(?:^|.*\D)(\d{8})(?:\D.*|$)' -and
$Matches[1] -gt ('2021/11/28"' -replace '/')
}
# Print the names of the filtered files.
$resultFiles.Name
$_.Name -match '(?:^|.*\D)(\d{8})(?:\D.*|$)'
看起来(最后一)运行的确切8个数字在每个文件的名称通过捕组((...)
),反映在自动 $Matches
变量的条目索引 1
($Matches[1]
)之后,如果发现。
'2021/11/28"' -replace '/'
删除所有 /
人物从输入串,使日期的格式串的相同。 为简洁起见,本方案上述执行这种替换 在每个环操作. 在实践中,你将执行这 一次,在循环,并将结果分配给一个变量使用内部循环。
Where-Object {$_.Name -match '(?:^|.*\D)(\d{8})(?:\D.*|$)' -and $Matches[1] -eq (Get-Date).Date}
(Get-Date).Date
与 (Get-Date).ToString('yyyyMMdd')
. 如果需要进一步的帮助,请建立一个新的问题员额。