Robocopy Exit Codes Explained: Below 8 Is Success (Tested)

スポンサーリンク
Robocopy Exit Codes Explained: Below 8 Is Success (Tested) File Operations
スポンサーリンク

I use robocopy to copy my PC’s D drive to a NAS, plus Windows backups to another drive. Both scripts show “there were errors” only when robocopy’s exit code is 8 or higher.

robocopy returns a non-zero exit code even when it copied everything fine. If you treat it like a normal command, “non-zero means error”, a successful copy gets reported as a failure.

Short answer: a robocopy exit code below 8 means success (no files failed to copy); 8 or higher means something failed. Microsoft’s documentation says the same: a value of 8 or greater indicates at least one failure during the copy.

robocopy exit codes

A robocopy exit code is the sum of these values. For example, 3 is “1 (files copied) + 2 (extra files at the destination)”.

Value Meaning Verdict
0 Nothing to copy. Source, destination already identical Success
1 One or more files copied Success
2 Extra files or folders at the destination that aren’t in the source (Extras) Success
4 Mismatched files or folders, e.g. the same name is a file in the source but a folder at the destination Success (check it)
8 Some files or folders couldn’t be copied (retry limit exceeded) Failure
16 Serious error. Nothing copied. A mistyped option, an inaccessible source, an inaccessible destination Failure

Microsoft’s robocopy - Microsoft Learn, Return codes that are used by the Robocopy utility - Microsoft Learn explain the combinations of 0-8, plus “8 or higher is a failure”. For the individual meanings of 4, 16, I referred to a Microsoft engineer’s blog post, Robocopy exit codes - Microsoft Learn (archive).

The combined values you’ll see:

Value Breakdown Meaning
3 1+2 Copied; extra files at the destination too
5 1+4 Copied; mismatched files too
6 2+4 Extra files, mismatched files; nothing copied
7 1+2+4 Copied; extra files, mismatched files too
9-15 8+α Some files copied, some failed

/MIR, /PURGE delete extra files at the destination. Those deletions still count as 2 (test 7 below).

Producing each exit code for real

To check, I reproduced each case on my PC (Windows 11 Pro) using test folders. Every run used /R:0 /W:0 (no retries); PowerShell’s $LASTEXITCODE gave the exit code.

What I did Exit code
Copied 2 new files 1
Ran again (no changes) 0
Put a file only at the destination, then ran 2
Added 1 new file, with a destination-only file present 3
Made a folder at the destination named like a source file (mismatch) 4
Copied an updated file while the destination file was held open by another process 8
Deleted a destination-only file with /MIR 2
Used a nonexistent folder as the source 16
Added a nonexistent option (/XYZ) 16

The output when the file was held open (exit code 8), paths shortened. The FAILED column in the summary shows 1:

	    Newer     		      13	a.txt
2026/11/08 19:41:21 ERROR 32 (0x00000020) Copying File D:\test\src\a.txt
The process cannot access the file because it is being used by another process.
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         4         0         3         0         1         0

With no source, it printed ERROR 2 (0x00000002) Accessing Source Directory; with a mistyped option, ERROR : Invalid Parameter #3 : "/XYZ". Both ended with 16 having copied nothing. An unreachable NAS or drive, making the source or destination inaccessible, should also give 16, but I didn’t test that.

Checking for 8 or higher in a batch file

In a batch file, use if errorlevel 8. As Microsoft’s if - Microsoft Learn says, errorlevel 8 is true when the exit code is 8 or higher, not exactly 8, so 9, 16 count too.

@echo off
robocopy "D:\data" "E:\backup\data" /MIR /R:3 /W:3 /log+:"E:\backup\robocopy.log" /np
if errorlevel 8 (
  echo Some files failed to copy. Check the log.
  exit /b 1
)
echo Done.
exit /b 0

You can write the same thing with %errorlevel%. GEQ is the “at least” comparison.

if %errorlevel% GEQ 8 echo Some files failed to copy.

Two caveats:

  • if errorlevel looks at the exit code of the program run right before it. Check immediately after robocopy, with nothing else in between
  • Inside ( ), %errorlevel% expands to its value from before the block. When running robocopy, checking the result inside ( ), use if errorlevel 8 (the batch file in robocopy hides the destination folder: cause, fix has this form)

Checking for 8 or higher in PowerShell

In PowerShell, an external program’s exit code goes into $LASTEXITCODE (about_Automatic_Variables - Microsoft Learn). Save it to a variable right after robocopy, then compare with -ge 8.

robocopy "D:\data" "E:\backup\data" /MIR /R:3 /W:3 /log+:"E:\backup\robocopy.log" /np
$rc = $LASTEXITCODE
if ($rc -ge 8) {
    Write-Host "Some files failed to copy (exit code $rc). Check the log." -ForegroundColor Red
    exit 1
}
Write-Host "Done (exit code $rc)." -ForegroundColor Green
exit 0

Avoid $?. It turns False whenever an external program’s exit code is non-zero, so even a successful copy (exit code 1) gets judged a “failure”.

The script I use for backing up to my NAS checks it this way too (Back up a local folder to a password-protected NAS with Robocopy).

Keeping Task Scheduler from showing “failed”

Run robocopy from Task Scheduler or a job tool, and even exit code 1 (files copied) shows “Last Run Result” as (0x1), looking like a failure. As in the batch, PowerShell examples above, return exit /b 0 / exit 0 below 8, exit /b 1 / exit 1 at 8 or higher, so it’s non-zero only on a real failure.

At 8 or higher, check “FAILED” in the log

The exit code only tells you something failed. Which file failed is in the log you kept with /log+:. If the FAILED column in the final summary isn’t 0, the log has lines marked ERROR giving the file name plus the reason (access denied, in use by another process, etc.).

Failure causes I actually hit:

  • “ERROR 1314 (0x00000522) … A required privilege is not held by the client.” when copying to a NAS with /COPYALL (covered in the NAS backup post)
  • “ERROR : You do not have the Manage Auditing user right.” when not run as administrator (covered in the hidden destination folder post)

References

コメント

Copied title and URL