我有一個特定的日期“2021/11/28”,我想要檔案名大于 2021/11/28 的示例檔案名(下面)中的檔案串列。不要記住檔案名的創建時間。
"test_20211122_aba.*"
"abc_20211129_efg.*"
"hij_20211112_lmn.*"
"opq_20211130_rst.*"
我期待得到
"abc_20211129_efg.*"
"opq_20211130_rst.*"
真的很感謝你的幫助。
uj5u.com熱心網友回復:
您并不嚴格需要將您的字串決議為日期([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 '/'/從輸入字串中洗掉所有字符,使日期字串的格式相同。為簡潔起見,上述解決方案在每個回圈操作中執行此替換。實際上,您將在回圈之前執行一次,并將結果分配給一個變數以供在回圈內使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366198.html
