Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:15:51+00:00 2026-05-23T12:15:51+00:00

I am just starting to learn how to script. I’m trying to understand how

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:15:51+00:00Added an answer on May 23, 2026 at 12:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im just starting to learn flex and im trying to understand how Flex does
I'm just starting to learn Objective-C, one thing I'm trying to learn is good
A friend is just starting to learn Java, using IntelliJ. He asks how can
Hi just starting to learn this language, more of a javascript/PHP guy... I can't
Hi I'm just starting to learn C#. I am trying to restore a .bak
I'm just starting to learn flex and AS3 I'm trying to get information into
Just starting to learn scala.. I can't seem to figure out how to compile
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
im just starting to learn LINQ and at the same time im working on
I am just starting to learn javascript, so I don't have the skills to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.