2017-03-27 1 views
-1

아래 예에서 x 일보다 오래된 파일을 삭제하고 싶습니다. 아래에서 사용하려고했지만 작동하지 않거나 오류가 발생했습니다.x 일보다 오래된 파일을 삭제하려면 어떻게해야합니까?

Get-ChildItem –Path “E:\del” –Recurse | Where-Object{$_.CreationTime –lt(Get-Date).AddDays(-5)} | Remove-Item 
+3

당신은 알고 잘 사용 했어요. 어떻게 된 거예요? – Clijsters

+0

버전 1을 가지고 있고 그것이 존재하는 디렉토리에 대해 혼란스럽지 않으십니까? http://stackoverflow.com/questions/1825585/determine-installed-powershell-version? – Matt

+0

@Clijsters _는 그가 Where-object' 절이 예상대로 작동하지 않는다는 것을 알려주는 error_를 던지지 않았습니다. – Matt

답변

1

여기 접근법이 있습니다.

$Path = E:\del 
$DaysBack = "-5" 
$CurrentDate = Get-Date 
$DatetoDelete = $CurrentDate.AddDays($DaysBack) 

#delete files from $Path directory that are older than $Daysback 
Get-ChildItem -Path $Path -Include * -Recurse | Where-Object {$_.LastWriteTime -lt $DatetoDelete} | Remove-Item -ErrorAction SilentlyContinue -Recurse -Force 
-1

이 시도 :

$cleanup_days = 5 
$cleanup_lastWrite = $now.AddDays(-$cleanup_days) 

Get-ChildItem -Path "E:\del" | Where-Object { $_ -is [System.IO.FileInfo] } | ForEach-Object { 
    If ($_.LastWriteTime -lt $cleanup_lastWrite) 
    { 
     Remove-Item $("E:\del\" + $_) 
    } 
} 
+0

$ now를 선언해야합니다. 그렇지 않으면 오류가 발생합니다. 다른 변수와 함께'$ now = Get-Date'를 추가하십시오. –