如何测试应用程序是返回成功还是失败

时间:2016-08-09 00:45:04

标签: batch-file error-code

我编写了一个批处理文件,在退出应用程序后重新启动它。但我只想在戒烟是故意的时候发生这种情况。如果应用程序崩溃,我希望批处理文件除了退出之外什么都不做。

如何在退出时测试应用程序是返回成功还是失败错误代码?这有意义吗?

感谢。

[编辑]

我试过了:

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

但它总是说错误级别为零,即使游戏崩溃。

1 个答案:

答案 0 :(得分: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

通常,关闭应用程序没有错误会给你errorlevel = 0.希望它有所帮助。

相关问题