Search code examples
batch-filecmd

How to Let CMD Know a Batch Script Failed?


This is example.bat:

@echo ERROR
@exit /b 1

In cmd.exe, execute this:

example.bat && (echo successful!) || echo failed!

Got:

ERROR
successful!

Is there a way to let cmd know example.bat failed and therefore print below output?

ERROR
failed!

Solution

  • This is a bug/side effect of cmd.exe, if a batch file is started without call or cmd /c.

    These variations work as expected

    call example.bat && echo successful! || echo failed!
    cmd /c example.bat && echo successful! || echo failed!