2

I'm writing a batch file on windows 7 that executing some commands one after another:

copy source1 dest1
call someFile1.bat
copy source2 dest2
call someFile2.bat
copy source3 dest3

Is there a way to return error code 1 if any of the commands returns error code 1?

Thanks!

McMendel
  • 105
  • 11
  • 1
    `exit /b %ERRORLEVEL%` will return from a `call` with the same exit code as the most recent command exited. Then you can just `call someFile2.bat || exit /b` if `someFile2.bat` exits non-zero. This will require modifying someFile1.bat and someFile2.bat, adding error handling where needed. – rojo May 12 '15 at 14:26
  • For future readers, I found my answer here: https://stackoverflow.com/questions/734598/how-do-i-make-a-batch-file-terminate-upon-encountering-an-error?noredirect=1 – R2RT Mar 16 '21 at 15:09

1 Answers1

2

Yes, there is a way. The code must check the return code.

SET EXITCODE=0

copy source1 dest1
call someFile1.bat
if ERRORLEVEL 1 goto Failed
copy source2 dest2
call someFile2.bat
if ERRORLEVEL 1 goto Failed
copy source3 dest3
goto Success

:Failed
set EXITCODE=1
:Success
EXIT /B %EXITCODE%

If you want to check to see that the COPY commands work, that will be another if statement.

lit
  • 14,456
  • 10
  • 65
  • 119
  • Thanks! I do needed also checking the copy. not the prettiest script i've written, but worked like a charm :) – McMendel May 13 '15 at 08:43