Pass Exit Code (errorlevel) From One Batch Script to Another

It is often helpful to pass the exit code (errorlevel) from one batch script to another. You can do this using exit %errorlevel%, which closes the batch script and returns the errorlevel. You are then free to evaluate the error level: echo %errorlevel%. Here is a simple robocopy script that returns an errorlevel to another batch script:

@echo off

robocopy.exe c:\data c:\backup *.bak /MIR /W:10 /R:1000000

echo Robocopy Return Code (errorlevel): %errorlevel%

if %errorlevel%==0 GOTO success
if %errorlevel%==1 GOTO success
if %errorlevel%==2 GOTO success
if %errorlevel%==3 GOTO success
if %errorlevel%==5 GOTO success
if %errorlevel%==6 GOTO success
if %errorlevel%==7 GOTO success
exit %errorlevel%

:success
SET errorlevel=0

Leave a Reply