6

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 If ERRORLEVEL 1 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 notepad.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 EXIT /B n

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 SET ERRORLEVEL= is a better method. My issue comes in when I try to start the app with

START "" notepad.exe 

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:

@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 

appstop.bat:

@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 

test.bat:

@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 ::**** set ERRORLEVEL=0 echo. echo ******** echo starting the service... echo. ::start "" "C:\Program Files\Microsoft Office\office11\winword.exe" start notepad.exe if ERRORLEVEL 1 goto error qprocess notepad.exe echo *Start.success* ERRORLEVEL is: %ERRORLEVEL% echo. goto end :stop ::**** :: Insert stopping stripts here... :: If there is an error, set ERRORLEVEL>1 ::**** set ERRORLEVEL=0 echo. echo ******** echo stopping the service... echo. qprocess notepad.exe taskkill /f /im notepad.exe if ERRORLEVEL 1 goto noProcess goto end :noProcess set ERRORLEVEL=2 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. set ERRORLEVEL=1 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... set ERRORLEVEL=1 echo. echo '%1' is an invalid parameter. echo Usage: %0 [-stop ^| -start] echo *wrongParams* ERRORLEVEL is now: %ERRORLEVEL% echo. exit /b 1 :end 

error.bat:

@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 
1
  • If you are running the code, this is where I get an error. If I use start, it fires up notepad just fine without any errors. If I use stop, it stops notepad without any errors. If I use stop again, it throws an error as expected. Now, if I use start, it should start up notepad without any errors again. However, the if statement after the START command is true and it goes to an error. I'm not sure why this is happening. Please help! Commented Jun 27, 2011 at 20:48

1 Answer 1

12

You should never SET the %errorlevel% variable. You are correct that there is a difference; The errorlevel that you get from an exiting process is an internal register that you can read with the %errorlevel% syntax. However, if you create a variable named ERRORLEVEL, it will mask the internal register and you lose access to the exit codes.

If you need to set the errorlevel register to a specific value, you can do it with the following command:

%comspec% /c exit %value% 

This will spawn a process which immediately exits with the desired code.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! I have been able to run the scripts without any unexpected errors. I tried not setting the ERRORLEVEL directly before, but I couldn't get it to reset before I ran the next command. But now I set the ERRORLEVEL back to zero using the command you gave in the error.bat script after it finishes doing the error handling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.