DROTHISM

Robocopy Exit Codes

Posted by Drothism on Sep 10th, 2008

Every once in a while I go looking for these return codes and end up spending several minutes searching the Internet for them. Of course, this time I ended up finding them in the obvious place - the README for Robocopy. Here they are, for posterity.

The return code from Robocopy is a bit map, defined as follows:

Hex Bit Value Decimal Value Meaning If Set
0×10 16 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.
0×08 8 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
0×04 4 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
0×02 2 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
0×01 1 One or more files were copied successfully (that is, new files have arrived).
0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

You can use this information in a batch file to report the most serious anomalies, as follows:

if errorlevel 16 echo ***FATAL ERROR*** & goto end
if errorlevel 8 echo **FAILED COPIES** & goto end
if errorlevel 4 echo *MISMATCHES* & goto end
if errorlevel 2 echo EXTRA FILES & goto end
if errorlevel 1 echo Copy successful & goto end
if errorlevel 0 echo –no change– & goto end
:end

 

Alternatively, full details of the return code could be reported as follows:

if errorlevel 16 echo ***FATAL ERROR*** & goto end
if errorlevel 15 echo FAIL MISM XTRA COPY & goto end
if errorlevel 14 echo FAIL MISM XTRA & goto end
if errorlevel 13 echo FAIL MISM COPY & goto end
if errorlevel 12 echo FAIL MISM & goto end
if errorlevel 11 echo FAIL XTRA COPY & goto end
if errorlevel 10 echo FAIL XTRA & goto end
if errorlevel 9 echo FAIL COPY & goto end
if errorlevel 8 echo FAIL & goto end
if errorlevel 7 echo MISM XTRA COPY & goto end
if errorlevel 6 echo MISM XTRA & goto end
if errorlevel 5 echo MISM COPY & goto end
if errorlevel 4 echo MISM & goto end
if errorlevel 3 echo XTRA COPY & goto end
if errorlevel 2 echo XTRA & goto end
if errorlevel 1 echo COPY & goto end
if errorlevel 0 echo –no change– & goto end
:end

 

Here are some more examples using PowerShell.

function RobocopyExitCode([int]$exitcode)
{
 $retValue = “UNKNOWN”
 
 switch ($exitcode)
    {
        0 {$retValue = “NO CHANGE”; break}
        1 {$retValue = “COPY”; break}
        2 {$retValue = “EXTRA”; break}
        3 {$retValue = “EXTRA COPY”; break}
        4 {$retValue = “MISMATCH”; break}
        5 {$retValue = “MISMATCH COPY”; break}
        6 {$retValue = “MISMATCH EXTRA”; break}
        7 {$retValue = “MISMATCH EXTRA COPY”; break}
        8 {$retValue = “FAIL”; break}
        9 {$retValue = “FAIL COPY”; break}
        10 {$retValue = “FAIL EXTRA”; break}
        11 {$retValue = “FAIL EXTRA COPY”; break}
        12 {$retValue = “FAIL MISMATCH”; break}
        13 {$retValue = “FAIL MISMATCH COPY”; break}
        14 {$retValue = “FAIL MISMATCH EXTRA”; break}
        15 {$retValue = “FAIL MISMATCH EXTRA COPY”; break}
        16 {$retValue = “FATAL ERROR”; break} 
        default {”UNKNOWN”}
    }
   
    return $retValue
}

 

Capturing the RoboCopy exit codes is fairly simple…

robocopy.exe \\srcpath \\destpath /E /R:3 /W:5 /NP /LOG+:logfile.log
$robocopyMsg = RobocopyExitCode($lastexitcode)

Leave a reply

You must be logged in to post a comment.