Hello everyone.
I am just starting to learn how to script. I'm trying to understand how the system handles Error Levels and how they can be used in error handling. I know there is a difference between the environment variable %ERRORLEVEL% and the Error Level of the system. If I understand this correctly, then the [cpp]If ERRORLEVEL 1[/cpp] code would check the environment variable before it checks the error level of the previous command.
So, in my program I am trying to interface a startup/stop script that will start/stop all scripts of a given machine (for testing I'm just using one application winword.exe as an example). I have two wrapper scripts that will either start up or stop the applications by passing arguments to the independent script. If there is an error in the independent script, it will set the errorlevel using the [cpp]EXIT /B n[/cpp] command. Once control is returned to the calling script, it will go to an error handling script if the exit status is non-zero.
At first I was setting the %ERRORLEVEL% to zero manually and then testing for an error after a START or TASKKILL command. But then I read that clearing %ERRORLEVEL% with [cpp]SET ERRORLEVEL=[/cpp] is a better method. My issue comes in when I try to start the app with [cpp]START "" "C:\Path\to\winword.exe[/cpp] Whenever I test the errorlevel after this command it is always greater than or equal to 1 unless I use SET ERRORLEVEL=0 before I run the start command. I have inserted the code for the four scripts below. Any insight and advice would be greatly appreciated.
appstart.bat:
[cpp]@echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.bat
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -start
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstart.bat...
:: **** End Calls
goto end
:end
[/cpp]
appstop.bat:
[cpp]@echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.ba
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -stop
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstop.bat...
:: **** End Calls
goto end
:end
[/cpp]
test.bat:
[cpp]
@echo off
if "%1"=="-start" goto :start
if "%1"=="-stop" goto :stop
goto wrongParams
:start
::****
:: Insert start up stripts here...
:: If there is an error, set ERRORLEVEL=1
::****
echo.
echo ********
echo starting the service...
echo.
set ERRORLEVEL=
start "" "C:\Program Files\Microsoft Office\office11\winword.exe"
if ERRORLEVEL 1 goto error
qprocess winword.exe
echo *Start.success* Errorlevel is: %ERRORLEVEL%
echo.
goto end
:stop
::****
:: Insert stopping stripts here...
:: If there is an error, set ERRORLEVEL>1
::****
echo.
echo ********
echo stopping the service...
echo.
set ERRORLEVEL=
qprocess winword.exe
taskkill /f /im winword.exe
if ERRORLEVEL 1 goto noProcess
goto end
:noProcess
echo *noProcess* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 2
:error
:: Errorhandler. Log application status and cause of error here. Set
:: ERRORLEVEL > 1 before returning to caller.
echo.
echo **** Error handler inside test.bat ****
echo.
echo *error* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 1
:wrongParams
:: Output an error if the wrong parameters were passed to this script.
:: Maybe try to self correct the parameter...
echo.
echo '%1' is an invalid parameter.
echo Usage: %0 [-stop ^| -start]
echo *wrongParams* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 1
:end
[/cpp]
error.bat:
[cpp]@echo off
echo **** You have reached error.bat ****
echo Errorlevel inside of error.bat is: %ERRORLEVEL%
echo.
::*** Handle error...***
goto error%ERRORLEVEL%
:error2
echo The process could not be stopped for some reason.
goto end
:error1
echo The process had an error in start up.
::*** ***
goto end
:end
[/cpp]
I am just starting to learn how to script. I'm trying to understand how the system handles Error Levels and how they can be used in error handling. I know there is a difference between the environment variable %ERRORLEVEL% and the Error Level of the system. If I understand this correctly, then the [cpp]If ERRORLEVEL 1[/cpp] code would check the environment variable before it checks the error level of the previous command.
So, in my program I am trying to interface a startup/stop script that will start/stop all scripts of a given machine (for testing I'm just using one application winword.exe as an example). I have two wrapper scripts that will either start up or stop the applications by passing arguments to the independent script. If there is an error in the independent script, it will set the errorlevel using the [cpp]EXIT /B n[/cpp] command. Once control is returned to the calling script, it will go to an error handling script if the exit status is non-zero.
At first I was setting the %ERRORLEVEL% to zero manually and then testing for an error after a START or TASKKILL command. But then I read that clearing %ERRORLEVEL% with [cpp]SET ERRORLEVEL=[/cpp] is a better method. My issue comes in when I try to start the app with [cpp]START "" "C:\Path\to\winword.exe[/cpp] Whenever I test the errorlevel after this command it is always greater than or equal to 1 unless I use SET ERRORLEVEL=0 before I run the start command. I have inserted the code for the four scripts below. Any insight and advice would be greatly appreciated.
appstart.bat:
[cpp]@echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.bat
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -start
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstart.bat...
:: **** End Calls
goto end
:end
[/cpp]
appstop.bat:
[cpp]@echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.ba
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -stop
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstop.bat...
:: **** End Calls
goto end
:end
[/cpp]
test.bat:
[cpp]
@echo off
if "%1"=="-start" goto :start
if "%1"=="-stop" goto :stop
goto wrongParams
:start
::****
:: Insert start up stripts here...
:: If there is an error, set ERRORLEVEL=1
::****
echo.
echo ********
echo starting the service...
echo.
set ERRORLEVEL=
start "" "C:\Program Files\Microsoft Office\office11\winword.exe"
if ERRORLEVEL 1 goto error
qprocess winword.exe
echo *Start.success* Errorlevel is: %ERRORLEVEL%
echo.
goto end
:stop
::****
:: Insert stopping stripts here...
:: If there is an error, set ERRORLEVEL>1
::****
echo.
echo ********
echo stopping the service...
echo.
set ERRORLEVEL=
qprocess winword.exe
taskkill /f /im winword.exe
if ERRORLEVEL 1 goto noProcess
goto end
:noProcess
echo *noProcess* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 2
:error
:: Errorhandler. Log application status and cause of error here. Set
:: ERRORLEVEL > 1 before returning to caller.
echo.
echo **** Error handler inside test.bat ****
echo.
echo *error* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 1
:wrongParams
:: Output an error if the wrong parameters were passed to this script.
:: Maybe try to self correct the parameter...
echo.
echo '%1' is an invalid parameter.
echo Usage: %0 [-stop ^| -start]
echo *wrongParams* Errorlevel is now: %ERRORLEVEL%
echo.
exit /b 1
:end
[/cpp]
error.bat:
[cpp]@echo off
echo **** You have reached error.bat ****
echo Errorlevel inside of error.bat is: %ERRORLEVEL%
echo.
::*** Handle error...***
goto error%ERRORLEVEL%
:error2
echo The process could not be stopped for some reason.
goto end
:error1
echo The process had an error in start up.
::*** ***
goto end
:end
[/cpp]