我有一个非常大的文件夹(包含几个级别的子文件夹,总共数百万个文件)。我只想删除超过X天(例如10天)的文件。
我的下面的脚本适用于包含数千个文件的文件夹,但不适用于该大文件夹。有没有想过优化这个?谢谢 !
$tmpList = Get-ChildItem -Path $sourceFolder -Recurse
$fileObjects = $tmpList `
| Where-Object { !$_.PSIsContainer -and ($_.LastWriteTime -le $maxDateToProcess) } `
| Sort-Object -Property "LastWriteTime" -Descending
$allFiles = $fileObjects | Select -ExpandProperty "FullName"
Remove-Item -Path $allFiles
键入以下命令以删除过去30天内未修改的文件,然后按Enter键:
Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item