Search code examples
batch-fileexit-codeerrorlevel

check if command was successful in a batch file


How within a batch file to check if command

start "" javaw -jar %~p0/example.jar

was successful or produced an error?

I want to use if/else statements to echo this info out.


Solution

  • You can use

    if errorlevel 1 echo Unsuccessful
    

    in some cases. This depends on the last command returning a proper exit code. You won't be able to tell that there is anything wrong if your program returns normally even if there was an abnormal condition.

    Caution with programs like Robocopy, which require a more nuanced approach, as the error level returned from that is a bitmask which contains more than just a boolean information and the actual success code is, AFAIK, 3.