這個需求在 linux 用 find 寫,很方便
find /tmp/location -type f -mtime +15 -delete
在 Windows Powershell 的話,可以參考這篇 Delete files older than 15 days using PowerShell
$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"
# Delete files older than the $limit.
Get-ChildItem -Path $path -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 -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
這是使用 Get-ChildItem -Path $path
,搭配 Where-Object ,Where-Object 裏面是做判斷, $_
是每個項目,用每個項目的 CreationTime 去判斷,然後用 Remove-Item 去刪除。
沒有留言:
張貼留言