Skip to content

Commit

Permalink
Fix robocopy not releasing a directory after moving it (closes #2305)
Browse files Browse the repository at this point in the history
- Use System.Diagnostics.Process for better control
- Wait up to 1 second after moving a directory, so robocopy can terminate its threads
  • Loading branch information
r15ch13 committed Jul 9, 2018
1 parent 5fe75dd commit e2792f2
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit e2792f2

Please sign in to comment.