2016-06-28 2 views
0

폴더를 새 디렉토리로 복사 한 다음 현재 날짜를 기준으로 새 디렉토리의 이름을 바꾸는 powershell 스크립트가 있습니다.디렉토리 powershell의 이름을 바꿀 수 없습니다

Rename-Item : Access to the path '\\DPR320-W12-1600\PRTG\6212016 backup' is denied. 

나는이 때문에 내가하지 않기 때문에 그 라인이 실행되기 직전에 일어나고 복사에있을 것 같아요 : 복사 기능 그러나 나는 그것이 디렉토리의 이름을 바꿀가는 다음과 같은 오류 메시지가 큰 얻을 작품 해당 명령이 자체적으로 실행될 때 오류 메시지가 표시됩니다. 복사가 끝난 직후 폴더의 이름을 바꿀 수있는 방법이 있습니까? 어떤 도움이라도 대단히 감사하겠습니다. 아래 스크립트를 전체적으로 붙여 넣었습니다.

$targetdirectory = "\\DPR320-W12-1600\PRTG" 
$sourcedirectory = "C:\Users\Public\Documents\PRTG Traffic Grapher" 
$todaysdate=get-date 
$minusoneweek=$todaysdate.adddays(-7) 
$minusdate=($minusoneweek.Month).tostring(),($minusoneweek.day).tostring(),($minusoneweek.year).tostring() 
$todaysdatestring=($todaysdate.Month).tostring(),($todaysdate.day).tostring(),($todaysdate.year).tostring() 
$oldfilename=$minusdate[0]+$minusdate[1]+$minusdate[2]+" backup" 
$newfilename=$todaysdatestring[0]+$todaysdatestring[1]+$todaysdatestring[2]+" backup" 
$Threshold = (get-date).AddMonths(-18) 
$culture = [Globalization.CultureInfo]::InvariantCulture 
$count=0 


Get-ChildItem $sourcedirectory\config | Where-Object { 
    $_.PsIsContainer -and 
    $_.BaseName -match '\d{6}' -and 
    ([DateTime]::ParseExact($_.BaseName, 'yyyyMMdd', $null) -gt (Get-Date).AddDays(-7)) 
} |Copy-Item -Recurse -Force -Destination $targetdirectory\$oldfilename\config 

Copy-Item -Force $sourcedirectory\config.csv -Destination $targetdirectory\$oldfilename 
Copy-Item -Force $sourcedirectory\config.prtg -Destination $targetdirectory\$oldfilename 

rename-item $targetdirectory\$oldfilename $newfilename 

$CleanupList = Get-ChildItem $targetdirectory\$newfilename\config 

foreach ($DirName in $CleanupList) 
{ 
try { 
    If([datetime]::ParseExact($DirName.BaseName, 'yyyyMMdd', $culture) -lt $Threshold) 

{ 
    write-host [datetime]::ParseExact($DirName.BaseName, 'yyyyMMdd', $culture) 
    #Remove-Item $DirName.FullName -Force -Recurse 

} 

Write-Host $count 
$count=$count+1 

} 


catch { 

Write-Host $count 
$count=$count+1 

    continue 
} 

} 

답변

1
당신은 성공할 때까지 파일의 이름을 변경하려고합니다 while 루프에 넣을 수 있습니다

:

while($true){ 
    try { 
     rename-item $targetdirectory\$oldfilename $newfilename -ErrorAction Stop 
     # It will only reach the break statement if rename-item was successful 
     # The break statement will exit the while loop 
     break 
    } 
    catch { 
     write-host "couldn't rename file..." 
     start-sleep -s 1 
    } 
} 

이 후 성공하지 못한 경우도 카운터를 추가하고 휴식 할 수 있습니다 n 시도. 그렇지 않으면 예기치 않은 무언가가 파일을 열고 열어 두거나 액세스를 거부하는 등의 바이러스 백신 차단과 같이 파일을 무한정 다시 시도하는 결과가 발생할 수 있습니다.

관련 문제