Sign in with
Sign up | Sign in
Your question

Batch Programming and Error Handling and START Command

Last response: in Applications
Share
June 27, 2011 3:03:20 PM

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
  1. 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 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
  1. 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
  1. SET ERRORLEVEL=
is a better method. My issue comes in when I try to start the app with
  1. START "" "C:\Path\to\winword.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:
  1. @echo off
  2. :: Script for application Start
  3. set ERRORLEVEL=
  4. :: ****
  5. :: Additional Batch files will be executed from within this file
  6. :: Example:
  7. :: Call Appbat01.bat
  8. :: The called batch file should set ERRORLEVEL non-zero if error
  9. :: ****
  10.  
  11. call test.bat -start
  12. if ERRORLEVEL 1 (call error.bat)
  13. echo.
  14. echo Control was returned to appstart.bat...
  15. :: **** End Calls
  16. goto end
  17.  
  18. :end


appstop.bat:
  1. @echo off
  2. :: Script for application Start
  3. set ERRORLEVEL=
  4. :: ****
  5. :: Additional Batch files will be executed from within this file
  6. :: Example:
  7. :: Call Appbat01.ba
  8. :: The called batch file should set ERRORLEVEL non-zero if error
  9. :: ****
  10.  
  11. call test.bat -stop
  12. if ERRORLEVEL 1 (call error.bat)
  13. echo.
  14. echo Control was returned to appstop.bat...
  15. :: **** End Calls
  16. goto end
  17.  
  18. :end


test.bat:
  1. @echo off
  2. if "%1"=="-start" goto :start
  3. if "%1"=="-stop" goto :stop
  4. goto wrongParams
  5.  
  6. :start
  7. ::****
  8. :: Insert start up stripts here...
  9. :: If there is an error, set ERRORLEVEL=1
  10. ::****
  11. echo.
  12. echo ********
  13. echo starting the service...
  14. echo.
  15. set ERRORLEVEL=
  16. start "" "C:\Program Files\Microsoft Office\office11\winword.exe"
  17. if ERRORLEVEL 1 goto error
  18. qprocess winword.exe
  19. echo *Start.success* Errorlevel is: %ERRORLEVEL%
  20. echo.
  21. goto end
  22.  
  23. :stop
  24. ::****
  25. :: Insert stopping stripts here...
  26. :: If there is an error, set ERRORLEVEL>1
  27. ::****
  28. echo.
  29. echo ********
  30. echo stopping the service...
  31. echo.
  32. set ERRORLEVEL=
  33. qprocess winword.exe
  34. taskkill /f /im winword.exe
  35. if ERRORLEVEL 1 goto noProcess
  36. goto end
  37.  
  38. :noProcess
  39. echo *noProcess* Errorlevel is now: %ERRORLEVEL%
  40. echo.
  41. exit /b 2
  42. :error
  43. :: Errorhandler. Log application status and cause of error here. Set
  44. :: ERRORLEVEL > 1 before returning to caller.
  45. echo.
  46. echo **** Error handler inside test.bat ****
  47. echo.
  48. echo *error* Errorlevel is now: %ERRORLEVEL%
  49. echo.
  50. exit /b 1
  51.  
  52. :wrongParams
  53. :: Output an error if the wrong parameters were passed to this script.
  54. :: Maybe try to self correct the parameter...
  55. echo.
  56. echo '%1' is an invalid parameter.
  57. echo Usage: %0 [-stop ^| -start]
  58. echo *wrongParams* Errorlevel is now: %ERRORLEVEL%
  59. echo.
  60. exit /b 1
  61. :end

error.bat:
  1. @echo off
  2. echo **** You have reached error.bat ****
  3. echo Errorlevel inside of error.bat is: %ERRORLEVEL%
  4. echo.
  5. ::*** Handle error...***
  6. goto error%ERRORLEVEL%
  7.  
  8. :error2
  9. echo The process could not be stopped for some reason.
  10. goto end
  11. :error1
  12. echo The process had an error in start up.
  13. ::*** ***
  14. goto end
  15.  
  16. :end
June 30, 2011 4:49:44 PM

Hi everyone. My friends over at stackoverflow showed me a solution to the problem I was having. I knew that setting the %errorlevel% environment variable would mask the error level register, but I thought I could control it by setting it appropriately and consistently throughout my code. Apparently I was wrong in that assumption. They suggested that I use
  1. %comspec% /c exit %value%
to set the errorlevel register directly so that I can handle the errors appropriately. The updated code templates are listed below. If you have any other suggestions, they would be greatly appreciated.

appstart.bat
  1. @echo off
  2. :: Script for application Start
  3. :: ****
  4. :: Additional Batch files will be executed from within this file
  5. :: Example:
  6. :: Call Appbat01.bat
  7. :: The called batch file should set ERRORLEVEL non-zero if error
  8. :: ****
  9. call test.bat -start
  10. if ERRORLEVEL 1 (call error.bat)
  11. :: **** End Calls
  12. goto end
  13.  
  14. :end


appstop.bat
  1. @echo off
  2. :: Script for application Start
  3. :: ****
  4. :: Additional Batch files will be executed from within this file
  5. :: Example:
  6. :: Call Appbat01.ba
  7. :: The called batch file should set ERRORLEVEL non-zero if error
  8. :: ****
  9. call test.bat -stop
  10. if ERRORLEVEL 1 (call error.bat)
  11. :: **** End Calls
  12. goto end
  13.  
  14. :end


test.bat
  1. @echo off
  2. if "%1"=="-start" goto :start
  3. if "%1"=="-stop" goto :stop
  4. goto wrongParams
  5.  
  6. :start
  7. ::****
  8. :: Insert start up stripts here...
  9. :: If there is an error, set ERRORLEVEL=1
  10. ::****
  11. echo.
  12. echo ********
  13. echo starting the service...
  14. echo.
  15. ::start "" "C:\Program Files\Microsoft Office\office11\winword.exe"
  16. start notepad.exe
  17. if ERRORLEVEL 1 goto error
  18. qprocess notepad.exe
  19. echo.
  20. goto end
  21.  
  22. :stop
  23. ::****
  24. :: Insert stopping stripts here...
  25. :: If there is an error, set ERRORLEVEL>1
  26. ::****
  27. echo.
  28. echo ********
  29. echo stopping the service...
  30. echo.
  31. qprocess notepad.exe
  32. taskkill /f /im notepad.exe
  33. if ERRORLEVEL 1 goto noProcess
  34. goto end
  35.  
  36. :noProcess
  37. %comspec% /c exit 2
  38. goto end
  39. :error
  40. :: Errorhandler. Log application status and cause of error here. Set
  41. :: ERRORLEVEL > 1 before returning to caller.
  42. %comspec% /c exit 1
  43. goto end
  44.  
  45. :wrongParams
  46. :: Output an error if the wrong parameters were passed to this script.
  47. :: Maybe try to self correct the parameter...
  48. echo.
  49. echo '%1' is an invalid parameter.
  50. echo Usage: %0 [-stop ^| -start]
  51. echo.
  52. %comspec% /c exit 1
  53. :end


error.bat
  1. @echo off
  2. ::*** Handle error...***
  3. goto error%ERRORLEVEL%
  4.  
  5. :error2
  6. echo The process could not be stopped for some reason.
  7. %comspec% /c exit 0
  8. goto end
  9. :error1
  10. echo The process is already started.
  11. %comspec% /c exit 0
  12. ::*** ***
  13. goto end
  14.  
  15. :end
m
0
l
!