【发布时间】:2019-11-24 15:46:53
【问题描述】:
所以我在以
开头的文件夹中有文件1__
和
0__
示例文件
1__shal1absahal9182skab.php
0__abns1a3bshal54a4m5sb.php
我试图让我的 powershell 脚本只删除早于60 mins 的1__ 文件,而0__ 可以在每个360 mins 中删除。
这是我当前的代码
$limit = (Get-Date).AddMinutes(-360)
$path = "C:\cache"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
我的脚本目前将这两个文件视为相同,并在 360 minute intivals 上删除它们。
【问题讨论】:
-
看起来一个用户在没有解释的情况下否决了所有答案(尽管无法确定)。
标签: powershell glob get-childitem