Is it possible to ask ROBOCOPY to exit with an exit code that indicates success or failure?

I am using ROBOCOPY as part of my TeamCity build configurations, and having to add a step to just silence the exit code from ROBOCOPY seems silly to me.

Basically, I have added this:

EXIT /B 0

to the script that is being run.

However, this of course masks any real problems that ROBOCOPY would return.

Basically, I would like to have exit codes of 0 for SUCCESS and non-zero for FAILURE instead of the bit-mask that ROBOCOPY returns now.

Or, if I can't have that, is there a simple sequence of batch commands that would translate the bit-mask of ROBOCOPY to a similar value?

link|improve this question

feedback

4 Answers

up vote 3 down vote accepted

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.

link|improve this answer
feedback

I use this:

robocopy .....
call :REPORT_ERRORLEVEL
goto :EOF

:REPORT_ERRORLEVEL
echo.
if ERRORLEVEL 16 echo ***FATAL ERROR*** & goto :EOF
if ERRORLEVEL 8 echo **FAILED COPIES** & goto :EOF
if ERRORLEVEL 4 echo *MISMATCHES* & goto :EOF
if ERRORLEVEL 2 echo EXTRA FILES & goto :EOF
if ERRORLEVEL 1 echo Copy successful & goto :EOF
if ERRORLEVEL 0 echo –no change– & goto :EOF
link|improve this answer
feedback

From this page you can add a section to your batch file that uses the list of error codes to output the errors and run different sections of code:

if errorlevel 16 echo ***FATAL ERROR*** & goto end
if errorlevel 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end
if errorlevel 14 echo FAIL + MISMATCHES + XTRA & goto end
if errorlevel 13 echo OKCOPY + FAIL + MISMATCHES & goto end
if errorlevel 12 echo FAIL + MISMATCHES& goto end
if errorlevel 11 echo OKCOPY + FAIL + XTRA & goto end
if errorlevel 10 echo FAIL + XTRA & goto end
if errorlevel 9 echo OKCOPY + FAIL & goto end
if errorlevel 8 echo FAIL & goto end
if errorlevel 7 echo OKCOPY + MISMATCHES + XTRA & goto end
if errorlevel 6 echo MISMATCHES + XTRA & goto end
if errorlevel 5 echo OKCOPY + MISMATCHES & goto end
if errorlevel 4 echo MISMATCHES & goto end
if errorlevel 3 echo OKCOPY + XTRA & goto end
if errorlevel 2 echo XTRA & goto end
if errorlevel 1 echo OKCOPY & goto end
if errorlevel 0 echo No Change & goto end


:END
REM END OF BATCH FILE
link|improve this answer
feedback

TechNet suggests this one-liner to convert into a 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
link|improve this answer
1  
Nice. Needs brackets around the robocopy command, but saved me using a wrapper script. – TheCodeKing Mar 2 at 14:59
I couldn't get this one-liner to work as a Command Line build step in TeamCity. I had to move it to a separate line. I also added the /B argument to the exit command, although I don't think that was required. – MikeWyatt May 7 at 17:35
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.