0
我是PowerShell的新手,对此不甚了解。Powershell删除文件夹,如果所有文件比x天更旧
我正在寻找一种方法来删除一个文件夹和所有子文件夹,如果这个文件中的所有文件都超过x天。我有一个代码删除文件夹和所有子文件夹中的所有文件,但我不知道如何改变它的权利。
$Now = Get-Date
$Days = "30"
$TargetFolder = "C:\temp"
$Extension = "*.*"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {($_.CreationTime -le "$LastWrite") -and ($_.LastWriteTime -le "$LastWrite")}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "Red"
Remove-Item $Location.FullName | out-Null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}
在30天前创建或修改? – 4c74356b41
两者都必须得到尊重,因为如果您在2015年下载了文件,ModifieDate是2015年,并且您在2017/22/02将其移至了“C:\ temp”,那么创建日期是我不会将其删除。 –
复制一个文件不会修改创建日期或写入时间,不知道你想如何实现这一点 – 4c74356b41