/codes/cmd/robocopy-exit-codes
robocopy exit codes: 0, 1, 2, 3, 8 and 16 explained
- tool
- Command Prompt (cmd)
- versions
- Windows 10, Windows 11, Windows Server 2016+
- os
- windows
robocopy's exit code is a set of bit flags, not a plain success/failure value. 0 means nothing needed copying, 1 means files were copied, and any value below 8 is a successful run. 8 (some copies failed) and 16 (fatal error, nothing copied) are failures. Scripts and CI jobs that treat every non-zero code as an error fail on successful copies; test for 8 or higher instead.
The bits
| Bit | Meaning |
|---|---|
| 0 | No files copied; source and destination already in sync |
| 1 | One or more files copied successfully |
| 2 | Extra files or folders found in the destination (not in the source); nothing was copied for them |
| 4 | Mismatched files or folders found (for example a file in one side and a folder with the same name in the other) |
| 8 | Some files or folders could not be copied (failures after retries) |
| 16 | Fatal error: robocopy did not copy anything (bad source path, invalid parameters, access denied to the root) |
Combined values are sums: 3 = copied + extras, 5 = copied + mismatches, 7 = copied + mismatches + extras, 9 = copied + some failures.
See the codes
mkdir src
> src\a.txt echo one
> src\b.txt echo two
robocopy src dst /nfl /ndl /njh /njs /np > nul
echo first copy: %ERRORLEVEL%
robocopy src dst /nfl /ndl /njh /njs /np > nul
echo nothing to do: %ERRORLEVEL%
> dst\extra.txt echo only in dst
robocopy src dst /nfl /ndl /njh /njs /np > nul
echo extra file in dst: %ERRORLEVEL%
> src\c.txt echo three
robocopy src dst /nfl /ndl /njh /njs /np > nul
echo copied and extra: %ERRORLEVEL%
robocopy missing dst /nfl /ndl /njh /njs /np > nul
echo source missing: %ERRORLEVEL%Output
first copy: 1 nothing to do: 0 extra file in dst: 2 copied and extra: 3 source missing: 16
Use it in scripts
robocopy "C:\data" "D:\backup\data" /mir /r:2 /w:5 /log:backup.log
if %ERRORLEVEL% GEQ 8 (
echo robocopy failed with %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
exit /b 0if errorlevel 8 is an equivalent test (it is true for 8 or more). The final exit /b 0 matters: without it, the batch file ends with robocopy's code, so a scheduler or CI runner reports a successful copy with code 1 as a failure.
From PowerShell:
robocopy C:\data D:\backup\data /mir /r:2 /w:5
if ($LASTEXITCODE -ge 8) { throw "robocopy failed with exit code $LASTEXITCODE" }