我是powershell脚本的初学者,所以保持冷静^^
我想删除目录 d:\test 上的文件 我只删除了 15 天以上的文件,但我不想删除目录 d:\test 中另一个目录上的文件。
我的剧本
#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "15"
#----- define folder where files are located ----#
$TargetFolder = "d:\test"
#----- define extension ----#
$Extension = "*.bak"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Nomatch = "d:\test\ZZ - Archives","d:\test\ZZ - Cloture Paye"
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} |
Where-Object {$_.FullName -notmatch "$Nomatch"}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}