I have a batch file that’s calling the same executable over and over with different parameters. How do I make it terminate immediately if one of the calls returns an error code of any level?
Basically, I want the equivalent of MSBuild’s ContinueOnError=false
.
Nakilon
34.9k14 gold badges107 silver badges142 bronze badges
asked Apr 9, 2009 at 14:56
Josh KodroffJosh Kodroff
27.3k27 gold badges95 silver badges148 bronze badges
0
Check the errorlevel
in an if
statement, and then exit /b
(exit the batch file only, not the entire cmd.exe process) for values other than 0.
same-executable-over-and-over.exe /with different "parameters"
if %errorlevel% neq 0 exit /b %errorlevel%
If you want the value of the errorlevel to propagate outside of your batch file
if %errorlevel% neq 0 exit /b %errorlevel%
but if this is inside a for
it gets a bit tricky. You’ll need something more like:
setlocal enabledelayedexpansion
for %%f in (C:\Windows\*) do (
same-executable-over-and-over.exe /with different "parameters"
if !errorlevel! neq 0 exit /b !errorlevel!
)
Edit: You have to check the error after each command. There’s no global «on error goto» type of construct in cmd.exe/command.com batch. I’ve also updated my code per CodeMonkey, although I’ve never encountered a negative errorlevel in any of my batch-hacking on XP or Vista.
answered Apr 9, 2009 at 15:03
system PAUSEsystem PAUSE
37.1k20 gold badges62 silver badges59 bronze badges
11
Add || goto :label
to each line, and then define a :label
.
For example, create this .cmd file:
@echo off
echo Starting very complicated batch file...
ping -invalid-arg || goto :error
echo OH noes, this shouldn't have succeeded.
goto :EOF
:error
echo Failed with error #%errorlevel%.
exit /b %errorlevel%
See also question about exiting batch file subroutine.
answered Jan 22, 2012 at 21:58
6
The shortest:
command || exit /b
If you need, you can set the exit code:
command || exit /b 666
And you can also log:
command || echo ERROR && exit /b
answered Feb 20, 2014 at 15:26
Benoit BlanchonBenoit Blanchon
13.4k4 gold badges73 silver badges82 bronze badges
2
Here is a polyglot program for BASH and Windows CMD that runs a series of commands and quits out if any of them fail:
#!/bin/bash 2> nul
:; set -o errexit
:; function goto() { return $?; }
command 1 || goto :error
command 2 || goto :error
command 3 || goto :error
:; exit 0
exit /b 0
:error
exit /b %errorlevel%
I have used this type of thing in the past for a multiple platform continuous integration script.
answered Oct 18, 2017 at 14:55
Erik AronestyErik Aronesty
11.7k5 gold badges64 silver badges44 bronze badges
0
One minor update, you should change the checks for «if errorlevel 1» to the following…
IF %ERRORLEVEL% NEQ 0
This is because on XP you can get negative numbers as errors. 0 = no problems, anything else is a problem.
And keep in mind the way that DOS handles the «IF ERRORLEVEL» tests. It will return true if the number you are checking for is that number or higher so if you are looking for specific error numbers you need to start with 255 and work down.
answered Apr 9, 2009 at 15:30
1
I prefer the OR form of command, as I find them the most readable (as
opposed to having an if after each command). However, the naive way of
doing this, command || exit /b %ERRORLEVEL%
is wrong.
This is because batch expands variables when a line is first read, rather
than when they are being used. This means that if the command
in the line
above fails, the batch file exits properly, but it exits with return code 0,
because that is what the value of %ERRORLEVEL%
was at the start of the
line. Obviously, this is undesirable in our script, so we have to enable
delayed expansion, like so:
SETLOCAL EnableDelayedExpansion
command-1 || exit /b !ERRORLEVEL!
command-2 || exit /b !ERRORLEVEL!
command-3 || exit /b !ERRORLEVEL!
command-4 || exit /b !ERRORLEVEL!
This snippet will execute commands 1-4, and if any of them fails, it will
exit with the same exit code as the failing command did.
answered Aug 17, 2018 at 14:20
XarnXarn
3,4701 gold badge21 silver badges43 bronze badges
2
We cannot always depend on ERRORLEVEL, because many times external programs or batch scripts do not return exit codes.
In that case we can use generic checks for failures like this:
IF EXIST %outfile% (DEL /F %outfile%)
CALL some_script.bat -o %outfile%
IF NOT EXIST %outfile% (ECHO ERROR & EXIT /b)
And if the program outputs something to console, we can check it also.
some_program.exe 2>&1 | FIND "error message here" && (ECHO ERROR & EXIT /b)
some_program.exe 2>&1 | FIND "Done processing." || (ECHO ERROR & EXIT /b)
answered Nov 26, 2014 at 6:06
Amr AliAmr Ali
3,0501 gold badge16 silver badges11 bronze badges
No matter how I tried, the errorlevel always stays 0 even when msbuild failed. So I built my workaround:
Build Project and save log to Build.log
SET Build_Opt=/flp:summary;logfile=Build.log;append=true
msbuild "myproj.csproj" /t:rebuild /p:Configuration=release /fl %Build_Opt%
search for «0 Error» string in build log, set the result to var
FOR /F "tokens=* USEBACKQ" %%F IN (`find /c /i "0 Error" Build.log`) DO (
SET var=%%F
)
echo %var%
get the last character, which indicates how many lines contains the search string
set result=%var:~-1%
echo "%result%"
if string not found, then error > 0, build failed
if "%result%"=="0" ( echo "build failed" )
That solution was inspired by Mechaflash’s post at How to set commands output as a variable in a batch file
and https://ss64.com/nt/syntax-substring.html
answered Sep 5, 2017 at 15:13
1
@echo off
set startbuild=%TIME%
C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe c:\link.xml /flp1:logfile=c:\link\errors.log;errorsonly /flp2:logfile=c:\link\warnings.log;warningsonly || goto :error
copy c:\app_offline.htm "\\lawpccnweb01\d$\websites\OperationsLinkWeb\app_offline.htm"
del \\lawpccnweb01\d$\websites\OperationsLinkWeb\bin\ /Q
echo Start Copy: %TIME%
set copystart=%TIME%
xcopy C:\link\_PublishedWebsites\OperationsLink \\lawpccnweb01\d$\websites\OperationsLinkWeb\ /s /y /d
del \\lawpccnweb01\d$\websites\OperationsLinkWeb\app_offline.htm
echo Started Build: %startbuild%
echo Started Copy: %copystart%
echo Finished Copy: %TIME%
c:\link\warnings.log
:error
c:\link\errors.log
answered Sep 5, 2013 at 23:27
2
Wow! that is freaky!
I am able to reproduce the apparent bug from the command line console by running the following (note I use /Q
to turn ECHO OFF so output is simpler):
D:\test>cmd /q /c bug.cmd
before
first if
D:\test>echo %errorlevel%
0
I get the same behavior if I rename the script to «bug.bat»
I also get the expected return code of 1 if I remove the 2nd IF.
I agree, this seems to be a bug. Logically, I see no reason for the two similar scripts to yield different results.
I don’t have a full explanation, but I believe I understand an important component to the behavior: The batch ERRORLEVEL and the exit code do not refer to the same thing! Below is the documentation for the EXIT command. The important bit is the description of the exitCode parameter.
D:\test>exit /?
Quits the CMD.EXE program (command interpreter) or the current batch
script.
EXIT [/B] [exitCode]
/B specifies to exit the current batch script instead of
CMD.EXE. If executed from outside a batch script, it
will quit CMD.EXE
exitCode specifies a numeric number. if /B is specified, sets
ERRORLEVEL that number. If quitting CMD.EXE, sets the process
exit code with that number.
I think the average person (including myself) does not typically distinguish between the two. But CMD.EXE seems to be very finicky as to when the batch ERRORLEVEL is returned as the exit code.
It is easy to show that the batch script is returning the correct ERRORLEVEL, yet the ERRORLEVEL is not being returned as the CMD exit code. I display the ERRORLEVEL twice to demonstrate that the act of displaying it is not clearing the ERRORLEVEL.
D:\test>cmd /q /v:on /c "bug.cmd&echo !errorlevel!&echo !errorlevel!"
before
first if
1
1
D:\test>echo %errorlevel%
0
As others have pointed out, using CALL does cause the ERRORLEVEL to be returned as the exit code:
D:\test>cmd /q /c "call bug.cmd"
before
first if
D:\test>echo %errorlevel%
1
But that doesn’t work if another command is executed after the CALL
D:\test>cmd /q /v:on /c "call bug.cmd&echo !errorlevel!"
before
first if
1
D:\test>echo %errorlevel%
0
Note that the above behavior is strictly a function of CMD.EXE, having nothing to do with the script, as evidenced by:
D:\test>cmd /q /v:on /c "cmd /c exit 1&echo !errorlevel!"
1
D:\test>echo %errorlevel%
0
You could explicitly EXIT with the ERRORLEVEL at the end of the command chain:
D:\test>cmd /q /v:on /c "call bug.cmd&echo !errorlevel!&exit !errorlevel!"
before
first if
1
D:\test>echo %errorlevel%
1
Here is the same thing without delayed expansion:
D:\test>cmd /q /c "call bug.cmd&call echo %errorlevel%&exit %errorlevel%"
before
first if
1
D:\test>echo %errorlevel%
1
Perhaps the simplest/safest work around is to change your batch script to EXIT 1
instead of EXIT /B 1
. But that may not be practical, or desirable, depending on how others may use the script.
EDIT
I’ve reconsidered, and now think it is most likely an unfortunate design «feature» rather than a bug. The IF statements are a bit of a red herring. If a command is parsed after EXIT /B, within the same command block, then the problem manifests, even though the subsequent command never executes.
test.bat
@exit /b 1 & echo NOT EXECUTED
Here are some test runs showing that the behavior is the same:
D:\test>cmd /c test.bat
D:\test>echo %errorlevel%
0
D:\test>cmd /c call test.bat
D:\test>echo %errorlevel%
1
D:\test>cmd /v:on /c "call test.bat&echo !errorlevel!"
1
D:\test>echo %errorlevel%
0
It doesn’t matter what the 2nd command is. The following script shows the same behavior:
@exit /b 1 & rem
The rule is that if the subsequent command would execute if the EXIT /B were something that didn’t exit, then the problem manifests itself.
For example, this has the problem:
@exit /b 1 || rem
But the following works fine without any problem.
@exit /b 1 && rem
And so does this work
@if 1==1 (exit /b 1) else rem
- Overview
- Part 1 – Getting Started
- Part 2 – Variables
- Part 3 – Return Codes
- Part 4 – stdin, stdout, stderr
- Part 5 – If/Then Conditionals
- Part 6 – Loops
- Part 7 – Functions
- Part 8 – Parsing Input
- Part 9 – Logging
- Part 10 – Advanced Tricks
Today we’ll cover return codes as the right way to communicate the outcome of your script’s execution to the world. Sadly, even
skilled Windows programmers overlook the importance of return codes.
Return Code Conventions
By convention, command line execution should return zero when execution succeeds and non-zero when execution fails. Warning messages
typically don’t effect the return code. What matters is did the script work or not?
Checking Return Codes In Your Script Commands
The environmental variable %ERRORLEVEL%
contains the return code of the last executed program or script. A very helpful feature is
the built-in DOS commands like ECHO
, IF
, and SET
will preserve the existing value of %ERRORLEVEL%
.
The conventional technique to check for a non-zero return code using the NEQ
(Not-Equal-To) operator of the IF
command:
IF %ERRORLEVEL% NEQ 0 (
REM do something here to address the error
)
Another common technique is:
IF ERRORLEVEL 1 (
REM do something here to address the error
)
The ERRORLEVEL 1
statement is true when the return code is any number equal to or greater than 1. However, I don’t use this technique because
programs can return negative numbers as well as positive numbers. Most programs rarely document every possible return code, so I’d rather explicity
check for non-zero with the NEQ 0
style than assuming return codes will be 1 or greater on error.
You may also want to check for specific error codes. For example, you can test that an executable program or script is in your PATH by simply
calling the program and checking for return code 9009.
SomeFile.exe
IF %ERRORLEVEL% EQU 9009 (
ECHO error - SomeFile.exe not found in your PATH
)
It’s hard to know this stuff upfront – I generally just use trial and error to figure out the best way to check the return code of the program or
script I’m calling. Remember, this is duct tape programming. It isn’t always pretty, but, it gets the job done.
Conditional Execution Using the Return Code
There’s a super cool shorthand you can use to execute a second command based on the success or failure of a command. The first program/script must
conform to the convention of returning 0 on success and non-0 on failure for this to work.
To execute a follow-on command after sucess, we use the &&
operator:
SomeCommand.exe && ECHO SomeCommand.exe succeeded!
To execute a follow-on command after failure, we use the ||
operator:
SomeCommand.exe || ECHO SomeCommand.exe failed with return code %ERRORLEVEL%
I use this technique heavily to halt a script when any error is encountered. By default, the command processor will continue executing
when an error is raised. You have to code for halting on error.
A very simple way to halt on error is to use the EXIT
command with the /B
switch (to exit the current batch script context, and not the command
prompt process). We also pass a specific non-zero return code from the failed command to inform the caller of our script about the failure.
SomeCommand.exe || EXIT /B 1
A simliar technique uses the implicit GOTO label called :EOF
(End-Of-File). Jumping to EOF in this way will exit your current script with
the return code of 1.
SomeCommand.exe || GOTO :EOF
Tips and Tricks for Return Codes
I recommend sticking to zero for success and return codes that are positive values for DOS batch files. The positive values are a good idea
because other callers may use the IF ERRORLEVEL 1
syntax to check your script.
I also recommend documenting your possible return codes with easy to read SET
statements at the top of your script file, like this:
SET /A ERROR_HELP_SCREEN=1
SET /A ERROR_FILE_NOT_FOUND=2
Note that I break my own convention here and use uppercase variable names – I do this to denote that the variable is constant and should not
be modified elsewhere. Too bad DOS doesn’t support constant values like Unix/Linux shells.
Some Final Polish
One small piece of polish I like is using return codes that are a power of 2.
SET /A ERROR_HELP_SCREEN=1
SET /A ERROR_FILE_NOT_FOUND=2
SET /A ERROR_FILE_READ_ONLY=4
SET /A ERROR_UNKNOWN=8
This gives me the flexibility to bitwise OR multiple error numbers together if I want to record numerous problems in one error code.
This is rare for scripts intended for interactive use, but, it can be super helpful when writing scripts you support but you don’t
have access to the target systems.
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET /A errno=0
SET /A ERROR_HELP_SCREEN=1
SET /A ERROR_SOMECOMMAND_NOT_FOUND=2
SET /A ERROR_OTHERCOMMAND_FAILED=4
SomeCommand.exe
IF %ERRORLEVEL% NEQ 0 SET /A errno^|=%ERROR_SOMECOMMAND_NOT_FOUND%
OtherCommand.exe
IF %ERRORLEVEL% NEQ 0 (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
)
EXIT /B %errno%
If both SomeCommand.exe and OtherCommand.exe fail, the return code will be the bitwise combination of 0x1 and 0x2, or decimal 3. This return code tells
me that both errors were raised. Even better, I can repeatedly call the bitwise OR with the same error code and still interpret which errors were
raised.
<< Part 2 – Variables
Part 4 – stdin, stdout, stderr >>
Every scripting and programming language contains an error handler like Java contains try-catch
for error handling. In a Batch script, there is no direct way to do this, but we can create an error handler in the Batch script using a built-in variable of the Batch script name %ERRORLEVEL%
.
This article will show how we can create a Batch script to handle errors and failures. Also, we are going to some examples that make the topic easier.
Error Handling in Batch Script
When a command successfully executes, it always returns an EXIT CODE
that indicates whether the command successfully executed or failed to execute. So, to create an error handling in a Batch file, we can use that EXIT CODE
in our program.
You can follow below general format to create an error handler:
@Echo off
SomeCommand && (
ECHO Message for Success
) || (
ECHO Message for Failure or Error
)
We can also do that by checking the variable named %ERRORLEVEL%
. If the variable contains a value not equal to 0
, then there might be a problem or error when executing the command. To test the %ERRORLEVEL%
variable, you can follow the below example codes:
@ECHO off
Some Command Here !!!
IF %ERRORLEVEL% NEQ 0 (Echo Error found when running the command &Exit /b 1)
You must note that the keyword NEQ
means Not Equal. And the variable %ERRORLEVEL%
will only contain a non-zero value if there is a problem or error in the code.
An Example That Contains Errors
Below, we shared an example. We will run a Batch file named Your_file.bat
from a location.
We intentionally removed that file from the directory. So it’s an error command.
The code for our example will be:
@echo off
ECHO Running a Batch file
CD G:\BATCH\
CALL Your_file.bat
IF errorlevel 1 GOTO ERROR
ECHO The file run successfully.
GOTO EOF
:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1
:EOF
Now, as the file doesn’t exist in the directory, it will show an error, and you will get the below output when you run the code shared above.
Output:
Running a Batch file
The system cannot find the path specified.
'Your_file.bat' is not recognized as an internal or external command,
operable program or batch file.
The file didn't run successfully.
An Error-Free Code Example That Runs Successfully
In the example above, we made a mistake on the code intentionally to understand how the code works. If we correct it like below:
@echo off
ECHO Running a Batch file
CALL "G:\BATCH\Yourfile.bat"
IF errorlevel 1 GOTO ERROR
ECHO The file runs successfully.
GOTO EOF
:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1
:EOF
Then we will get an output like this:
Running a Batch file
This is from the first file
The file runs successfully.
Remember, all commands we discussed here are only for the Windows Command Prompt or CMD environment.
The correct name for errorlevels would be return codes.
But since the DOS command to determine the return code is IF ERRORLEVEL
, most people use the name errorlevel.
Errorlevels are not a standard feature of every command.
A certain errorlevel may mean anything the programmer wanted it to.
Most programmers agree that an errorlevel 0 means the command executed successfully, and an errorlevel 1 or higher usually spells trouble.
But there are many exceptions to this general rule.
IF ERRORLEVEL
construction has one strange feature, that can be used to our advantage: it returns TRUE if the return code was equal to or higher than the specified errorlevel.
This means most of the time we only need to check IF ERRORLEVEL 1 ...
and this will return TRUE for every positive, non-zero return code.
Likewise, IF NOT ERRORLEVEL 0 ...
will return TRUE for every negative, non-zero return code.
In CMD.EXE (Windows NT 4 and later) the old-fashioned DOS IF ERRORLEVEL ...
may sometimes fail, since executables may return negative numbers for errorlevels!
However, this can be fixed by using the following code to check for non-zero return codes:
IF %ERRORLEVEL% NEQ 0 ...
Use the code above wherever you would have used IF ERRORLEVEL 1 ...
in the «past».
Thanks for Noe Parenteau for this tip.
In COMMAND.COM (MS-DOS, DOS-box, Windows 9*/ME), to determine the exact return code the previous command returned, we could use a construction like this:
@ECHO OFF IF ERRORLEVEL 1 SET ERRORLEV=1 IF ERRORLEVEL 2 SET ERRORLEV=2 IF ERRORLEVEL 3 SET ERRORLEV=3 IF ERRORLEVEL 4 SET ERRORLEV=4 • • • IF ERRORLEVEL 254 SET ERRORLEV=254 IF ERRORLEVEL 255 SET ERRORLEV=255 ECHO ERRORLEVEL = %ERRORLEV%
This is perfectly OK if we only have to check, say, 15 consecutive errorlevels.
If we need to check every errorlevel, though, there are better alternatives.
(As I learned from Charles Long, in XP the SET command no longer sets an errorlevel itself.)
However, Windows NT 4 and later make it easy by storing the latest errorlevel in the environment variable ERRORLEVEL:
ECHO.%ERRORLEVEL%
will display the errorlevel.
This blog entry by Batcheero explains perfectly why you should never SET the ERRORLEVEL variable.
The safest way to use errorlevels for
all DOS versions is the reverse order check.Start checking the highest errorlevel that can be expected, then check for the one below, etcetera:
IF ERRORLEVEL 255 GOTO Label255
IF ERRORLEVEL 254 GOTO Label254
•
•
•
IF ERRORLEVEL 2 GOTO Label2
IF ERRORLEVEL 1 GOTO Label1
GOTO Label0
:Label255
(commands to be executed at errorlevel 255)
GOTO End
•
•
•
:Label1
(commands to be executed at errorlevel 1)
GOTO End
:Label0
(commands to be executed at errorlevel 0, or no errorlevel)
:End
This will result in many more lines of batch code, but at least it will work in any DOS version.
In Windows NT (Windows NT 4 … Windows 10) this may not suffice, though, because errorlevels can have negative integer values as well.
In DOS (COMMAND.COM), we can use FOR loops to determine the errorlevel:
@ECHO OFF REM Reset variables FOR %%A IN (1 10 100) DO SET ERR%%A= REM Check error level hundredfolds FOR %%A IN (0 1 2) DO IF ERRORLEVEL %%A00 SET ERR100=%%A IF %ERR100%==2 GOTO 200 IF %ERR100%==0 IF NOT "%1"=="/0" SET ERR100= REM Check error level tenfolds FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %ERR100%%%A0 SET ERR10=%%A IF "%ERR100%"=="" IF %ERR10%==0 SET ERR10= :1 REM Check error level units FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL %ERR100%%ERR10%%%A SET ERR1=%%A REM Modification necessary for errorlevels 250+ IF NOT ERRORLEVEL 250 FOR %%A IN (6 7 8 9) DO IF ERRORLEVEL %ERR100%%ERR10%%%A SET ERR1=%%A GOTO End :200 REM In case of error levels over 200 both REM tenfolds and units are limited to 5 REM since the highest DOS error level is 255 FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL 2%%A0 SET ERR10=%%A IF ERR10==5 FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL 25%%A SET ERR1=%%A IF NOT ERR10==5 GOTO 1 :End REM Clean up the mess and show results SET ERRORLEV=%ERR100%%ERR10%%ERR1% FOR %%A IN (1 10 100) DO SET ERR%%A= ECHO ERRORLEVEL %ERRORLEV%
This example still handles only 255 error levels (that’s all there is in DOS), but it can be easily adjusted once you understand the basic principles.
To check errorlevels during batch file development, use either COMMAND /Z yourbatch.bat to display the errorlevel of every command executed in MS-DOS 7.* (Windows 95/98), or PROMPT Errorlevel$Q$R$_$P$G in OS/2 Warp (DOS) sessions.
Setting errorlevels
MS-DOS & Windows 9x:
Use ERRORLVL.EXE from OzWoz Software, or SETERLEV.COM 1.0 from Jim Elliott to test batch files that (are supposed to) check on errorlevels.
The syntax couldn’t be simpler:
ERRORLVL number
or
SETERLEV number
where number can be any number from 0 to 255.
A small Kix «one liner» can be used too:
EXIT $ErrLev
If called by a batch like this:
KIX32 ERRORLEVEL.KIX $ErrLev=23
it will return an errorlevel 23 (ERRORLEVEL.KIX would be the name of the kix script mentioned above).
Or use CHOICE.COM, available in all DOS 6.* and up versions, to set an errorlevel:
ECHO 5 | CHOICE /C:1234567890 /N
and
ECHO E | CHOICE /C:ABCDEFGHIJ /N
will both result in errorlevel 5 since both 5 and E are the fifth choice (/C:…) in the corresponding CHOICE commands.
Windows NT 4 and later:
In NT 4 use either
COLOR 00
or
VERIFY OTHER 2> NUL
to set an errorlevel 1.
Windows 2000 and later:
In Windows 2000 and later the EXIT
command accepts an optional exit code, a.k.a. return code or errorlevel, e.g. EXIT /B 1
for errorlevel 1:
EXIT
Quits the CMD.EXE program (command interpreter) or the current batch script.
EXIT [ /B ] [ exitCode ]
/B | Specifies to exit the current batch script instead of CMD.EXE. If executed from outside a batch script, it will quit CMD.EXE. |
exitCode | Specifies a numeric number. If /B is specified, sets ERRORLEVEL that number. If quitting CMD.EXE, sets the process exit code with that number. |
[ Brought to my attention by Maor Conforti. Thanks ]
If you want to set an errorlevel inside a batch file, for example to test an external command used by that batch file, you can use CMD.EXE /K EXIT 6
to set errorlevel 6 and continue.
Do NOT use SET ErrorLevel=6
as this will render the Errorlevel
variable static.
Related stuff
• Use EXIT in Windows 2000 (and later) to set errorlevels.
• See how errorlevels are used to check the availability of third party tools, and how your batch file can even help the user download the tool if it isn’t available.
• This blog entry by Batcheero explains perfectly why you should never SET the ERRORLEVEL variable.
The same goes for other dynamic environment variables like CD (current directory), DATE (current date), TIME (current time), RANDOM (random decimal number between 0 and 32767), CMDEXTVERSION (current Command Processor Extensions version) and CMDCMDLINE (the original command line that invoked the Command Processor).
page last modified: 2022-04-01