刪除過期檔案
-
2011年8月10日 上午 12:31
各位前輩好
小弟目前在學習powershell中
日前試著寫了一個刪除過期檔案的script,但執行都失敗
想請各位前輩指點一下
$tempfile=(Get-ChildItem "d:\temp" -recurse)
Where-Object {
$_.mode.startswith("-") -and (((get-date)-$_.LastWriteTime).hours -gt 10)
}
remove-item -path (Get-ChildItem "d:\temp" -recurse) -filter (((Get-ChildItem "o:\temp" -recurse ) -eq $tempfile) | Out-String -Stream) -Force小弟想達到的目的,是把d:\temp中每個資料夾裡過期(超過10天沒存取)的檔案刪除,再把資料夾刪除
但不知道remove-item這邊該怎麼寫比較好
所有回覆
-
2011年8月10日 下午 05:11
參考看看,由於沒有環境,因此請自行測試看看是否執行無誤:
$Today = Get-Date $Days = “-10” $TargetFolder = “D:\Temp” $LastWrite = $Today.AddDays($Days) $Files = Get-ChildItem $TargetFolder -Recurse | where {$_.LastWriteTime -le “$LastWrite”} foreach ($File in $Files) { “刪除檔案:$File”; Remove-Item $File -Force | Out-Null}
☞ 這裡是「免費的討論區」,付費支援服務請造訪此處,享受尊榮服務 ☜
如果回覆對您有幫助,請記得按下「
標示為解答」。 -
2011年8月12日 上午 04:49
感謝 Alex前輩
不過小弟執行後,會出現
Remove-Item : 無法將引數繫結到 'Path' 參數,因為它是 Null。
位於 行:9 字元:13
+ Remove-Item <<<< $File -Force | Out-Null}
+ CategoryInfo : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand
這樣的錯誤訊息 -
2011年11月4日 上午 05:24
$Today = Get-Date $Days = “-10” $TargetFolder = “D:\Temp” $LastWrite = $Today.AddDays($Days) Get-ChildItem $TargetFolder -Recurse | Where {!$_.PSIsContainer}| where {$_.LastWriteTime -le "$LastWrite"} | Remove-Item
- 已標示為解答 Cftzeng 2012年11月27日 上午 08:11

