1

I wrote a batch file that restarts an application after it is quitted. But I only want this to happen when the quitting was intentional. If the application crashed, I want the batch file to not do anything but exit.

How do I test whether an application returns success or failure error code upon exit? Does this make sense?

Thanks.

[edit]

I tried this:

@echo off
e:
cd %HWRM%\Bin\Release
HomeworldRM.exe
echo The errorlevel is %errorlevel%.
pause

But it always says the errorlevel is zero, even if the game crashes.

2
  • 2
    Do you have documentation that says the app will indicate success or failure when it exits? There is no requirement that it does so. And how is the app being quitted? – Ken White Aug 9 '16 at 1:02
  • There are no docs that mention this. Sometimes I quit the application with ALT+F4. Sometimes it quits itself due to the running of a script. Occasionally it crashes. The application is a game by the way. – posfan12 Aug 9 '16 at 3:00
2
@echo off

:startGame
yourGame.exe

rem simple test case1: abcde.exe
rem abcde.exe do not exist, errorlevel will not be 0

rem simple test case2: call cde.bat 
rem the bat only contain one line of code: exit /B 1
rem it will return errorlevel = 1

echo errorlevel is %errorlevel%

if %errorlevel% EQU 0 ECHO quitting intentional and restart now & pause & goto startGame

rem return TRUE when the ERRORLEVEL is greater than or equal to 1
if %errorlevel% GTR 0 ECHO Crashed and Bye
pause

Usually, closing application without error will give you errorlevel = 0. Hope it helps.

9
  • Will using the start command affect the errorlevel? E.g.: start "a" /B /WAIT HomeworldRM.exe The reason I ask is because the game is returning errorlevel 0 all the time, even when crashing. – posfan12 Aug 9 '16 at 4:12
  • It do affect. I have tried start command with /B, it will still create a new context. Errorlevel cannot be return by start command. I tested that call bat and simply type the exe are both okay. – Pika Aug 9 '16 at 7:11
  • It is not working for me, even without the start command. Is there an alternate solution without using batch files maybe? – posfan12 Aug 9 '16 at 21:51
  • According to previous comment, it really depends on how is the app being quitted? – Pika Aug 10 '16 at 2:20
  • What about my previous response to that question do you not understand? – posfan12 Aug 10 '16 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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