title: Batch Programming, Error Handling, and Start Command
body: <p>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
  <code>If ERRORLEVEL 1</code>
  code would check the environment variable before it checks the error level of the previous command. </p>
  
  <p>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
  <code>EXIT /B n</code></p>
  
  <p>command. Once control is returned to the calling script, it will go to an error handling script if the exit status is non-zero. </p>
  
  <p>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
  <code>SET ERRORLEVEL=</code>
  is a better method. My issue comes in when I try to start the app with</p>
  
  <pre><code>START "" notepad.exe
  </code></pre>
  
  <p>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. </p>
  
  <p>appstart.bat:</p>
  
  <pre><code>@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
  </code></pre>
  
  <p>appstop.bat:</p>
  
  <pre><code>@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
  </code></pre>
  
  <p>test.bat:</p>
  
  <pre><code>@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&gt;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 &gt; 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
  </code></pre>
  
  <p>error.bat:</p>
  
  <pre><code>@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
  </code></pre>