小编Ron*_*ina的帖子

按文件名 Powershell 中的日期列出文件

我希望你一切都好。

我是 Powershell 的新手,正在尝试制作一个脚本,允许根据文件名中的日期列出 7 天前的文件,然后将它们复制/移动到另一个目录。

文件结构:2020_06_11_DB.txt YYYY_MM_dd

我设置了这段脚本,但它只将文件与参考日期(DateRef)相匹配,而不是至少(旧):

$SourceDB = "C:\DB\"
$DestinationDB = "C:\Destination\DB\"
$DateRef = Get-Date ((Get-Date).AddDays(-7)) -Format yyyy_MM_dd

$Query = Get-ChildItem $SourceDB -Filter "*$DateRef*" |
ForEach-object {
    Copy-Item -Path $_.FullName -Destination $DestinationDB
}
Run Code Online (Sandbox Code Playgroud)

我尝试过像 -le 和 -lt 这样的运算符,但没有成功。如果你能帮助我,我将不胜感激。

Get-ChildItem $SourceDB -Filter '*.txt' | Where-Object {$_.BaseName -le "*$DateRef*"}
Run Code Online (Sandbox Code Playgroud)

谢谢!

powershell filenames get-childitem

2
推荐指数
1
解决办法
116
查看次数

标签 统计

filenames ×1

get-childitem ×1

powershell ×1