Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
It should also be noted that the first 8 exit codes (0-7) are apparently not error states: stackoverflow.com/questions/16533843/psake-and-robocopy-failing –  longda Feb 12 at 22:34
add comment

6 Answers

up vote 16 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.

share|improve this answer
add comment

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
share|improve this answer
5  
Nice. Needs brackets around the robocopy command, but saved me using a wrapper script. –  TheCodeKing Mar 2 '12 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 '12 at 17:35
    
For Teamcity deployment (and not only Teamcity) it is useful to type: IF %ERRORLEVEL% LEQ 3 set errorlevel=0 and on the next line: if %errorlevel% neq 0 exit /b %errorlevel% (if batch file consists of several operations, not only robocopy), because OK codes are less then 3. ss64.com/nt/robocopy-exit.html –  DaoCacao Dec 11 '12 at 13:23
1  
In TeamCity, you should escape the ERRORLEVEL with double %%, like this: %%ERRORLEVEL%%. Otherwise, it considers it to be TeamCity build parameter. –  Yan Sklyarenko Sep 27 '13 at 7:09
add comment

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
share|improve this answer
add comment

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
share|improve this answer
add comment

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
share|improve this answer
add comment

Some posters above have missed the subtlety of the bit mask. In particular paradroid has missed that errorlevel 3 indicates a completely successful copy.

Note that bit 0x01 if set indicates that some files have been copied even if there were other failures. So any odd numbered errorlevels always indicate that at least some files have been copied. Note also that bit 0x02 simply indicates that there are files at the destination that are not present at the source. This will happen if the /E switch is used and files have been deleted from the source since a previous copy was taken. It should not happen if the /MIR switch is used because that should delete files at the destination to mirror the source (but I haven't tested this).

So both errorlevel 1 AND 3 indicate successful copying of files with no errors. Also errorlevels 0 AND 2 indicate that the destination is up to date and no files were copied.

For what its worth I came up with the following for my simple backup:

if errorlevel 16 echo Backup failed - see reason above & goto done

if errorlevel 8 echo All is not well - backup incomplete & goto done

if errorlevel 4 echo All is not well - some files were mismatched & goto done

if errorlevel 3 echo Backup completed successfully & goto done

if errorlevel 2 echo Backup already up to date - no files copied & goto done

if errorlevel 1 echo Backup completed successfully & goto done

if errorlevel 0 echo Backup already up to date - no files copied & goto done

I chose not to bother about the 'extra' files.

I have no idea what the 'mismatched' error is because it hasn't happened yet but I allowed for it just in case.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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