From e2792f2e02adee5947ebb95022a62282fb61024f Mon Sep 17 00:00:00 2001 From: Richard Kuhnt Date: Mon, 9 Jul 2018 12:34:00 +0200 Subject: [PATCH] Fix robocopy not releasing a directory after moving it (closes #2305) - Use System.Diagnostics.Process for better control - Wait up to 1 second after moving a directory, so robocopy can terminate its threads --- lib/core.ps1 | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 199d7d6b2..59642754a 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -244,9 +244,28 @@ function movedir($from, $to) { $from = $from.trimend('\') $to = $to.trimend('\') - $out = robocopy "$from" "$to" /e /move - if($lastexitcode -ge 8) { - throw "Could not find '$(fname $from)'! (error $lastexitcode)" + $proc = New-Object System.Diagnostics.Process + $proc.StartInfo.FileName = 'robocopy.exe' + $proc.StartInfo.Arguments = "$from $to /e /move" + $proc.StartInfo.RedirectStandardOutput = $true + $proc.StartInfo.RedirectStandardError = $true + $proc.StartInfo.UseShellExecute = $false + $proc.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden + $proc.Start() + $out = $proc.StandardOutput.ReadToEnd() + $proc.WaitForExit() + + if($proc.ExitCode -ge 8) { + debug $out + throw "Could not find '$(fname $from)'! (error $($proc.ExitCode))" + } + + # wait for robocopy to terminate its threads + 1..10 | ForEach-Object { + if (!(Test-Path $from)) { + break + } + Start-Sleep -Milliseconds 100 } }