Getting ROBOCOPY to return a "proper" exit code?
Solution 1:
As per here, Robocopy has the following exit code bits that make up the exit code:
0×10 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 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
0×04 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
0×02 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
0×01 One or more files were copied successfully (that is, new files have arrived).
0×00 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.
Just add if/else statements that EXIT /B 0
when the return value is 1 or maybe 0, and EXIT /B 1
otherwise. Even if files might have been copied, there's something wrong that would need manual intervention.
Solution 2:
TechNet suggests this one-liner to convert the exit code into a more traditional exit code:
(robocopy c:\dirA c:\dirB *.*) ^& IF %ERRORLEVEL% LEQ 1 exit 0
Or this to ignore the exit code completely (i.e. don't care if it failed or succeeded):
(robocopy c:\dirA c:\dirB *.*) ^& exit 0
However, both commands above will terminate a script after the robocopy has executed. This is an issue especially for CI builds. If you want to use robocopy in this scenario, you need to set the error code manually for irrelevant exit codes. Below, all error codes below 8 will be rewritten to no error at all, and the script will be continued if possible.
(robocopy c:\dirA c:\dirB *.*) ^& IF %ERRORLEVEL% LSS 8 SET ERRORLEVEL = 0
Solution 3:
Running it from Jenkins needs both ( )
and /B
. If you want to ignore the error level 1,2,3,4:
(robocopy XXX YYY) ^& IF %ERRORLEVEL% LEQ 4 exit /B 0