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.

posfan12
  • 2,541
  • 8
  • 35
  • 57
  • 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 09 '16 at 01: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 09 '16 at 03:00

1 Answers1

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.

Pika
  • 507
  • 1
  • 6
  • 16
  • 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 09 '16 at 04: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 09 '16 at 07:11
  • It is not working for me, even without the `start` command. Is there an alternate solution without using batch files maybe? – posfan12 Aug 09 '16 at 21:51
  • According to previous comment, it really depends on how is the app being _quitted_? – Pika Aug 10 '16 at 02:20
  • What about my previous response to that question do you not understand? – posfan12 Aug 10 '16 at 08:36
  • What is the details of _not working_ ? Also, how is the app being quitted is necessary for finding alternative solution. It is not an empty comment. – Pika Aug 10 '16 at 09:00
  • The game is either: 1) exited manually using an in-game menu; 2) exited suddenly by the user pressing ALT+F4 like many Windows programs; 3) exited intentionally as part of a running script but without human intervention; 4) crashes to desktop with a popup error message due to some sort of bug. All of these result in the same errorlevel apparently. – posfan12 Aug 10 '16 at 12:08
  • [how-to-check-if-a-process-is-running-via-a-batch-script](http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script) I have a idea like that Checking if a exe alive, if not, restart it. if you intentionally want to close this exe, you should close the bat first. – Pika Aug 12 '16 at 01:49
  • Yes, I do this already. BUT I do not want to restart it if the program crashed (due to a bug, etc.). – posfan12 Aug 12 '16 at 11:08