China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 00:40
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » [Sharing] How to set the ERRORLEVEL value in a DOS environment View 2,063 Replies 4
Original Poster Posted 2004-02-06 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
To All:

The ERRORLEVEL value in a DOS environment is generally produced when an external program exits, but in some situations we need to produce the errorlevel value ourselves.

The following batch files show several methods for setting ERRORLEVEL. Because of system-related factors, none of them have been tested. If anyone is willing to do so, I'd be very grateful for corrections!

Also, for a program to get the errorlevel value, see:
http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9358
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2004-02-06 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: Seterror.bat - First version
:: This is my first algorithm, implemented with pure batch processing, and it has many limitations
:: It can only set 0-9 and 255; all other values are treated as "large values" and set to 10
@echo off
if == goto end
if == goto setzero
if == goto setmax
for %%a in (1 2 3 4 5 6 7 8 9) do if == goto set1to9
goto setbig

:setzero
echo exist> test.txt
find "exist" test.txt > nul
del test.txt
goto end

:set1to9
choice /c:123456789 /t:%1,1 /n > nul
goto end

:setbig
choice /c:123456789a /t:a,1 /n > nul
goto end

:setmax
choice /d /n > nul
goto end

:end




※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2004-02-06 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::Seterror.bat - Second version
::Below is an assembly-language algorithm that calls subfunction 4Ch of DOS interrupt int21
::When using it, the first parameter must be a two-digit hexadecimal number
@echo off
if == goto end

:main
echo a>seterror.asd
echo mov ax,4c%1>>seterror.asd
echo int21>>seterror.asd
echo.>>seterror.asd
echo g>>seterror.asd
echo q>>seterror.asd
debug nul

:check
call geterror.bat @
if == if not == goto error
if not == echo Set current errorlevel is %el%
goto end

:error
echo.
echo Error: Invalid argument "%1"
echo.
goto end

:end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2004-02-07 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
:: Seterror.bat - Third version
:: This is an improved version of the assembly program, with radix conversion code added, so decimal parameters can be used
:: Handling of invalid parameters:
:: 22e-->22 e22-->0 256-->0 65535-->255 65536-->0
@echo off
if == goto end
if exist seterror.com goto run
if exist seterror.asd goto debug

:setscript
echo >seterror.asd A100
echo>>seterror.asd MOV SI,0082
echo>>seterror.asd XOR AX,AX
echo>>seterror.asd XOR BX,BX
echo>>seterror.asd MOV CX,000A
echo>>seterror.asd MOV BL,
echo>>seterror.asd CMP BL,30
echo>>seterror.asd JL 0120
echo>>seterror.asd CMP BL,39
echo>>seterror.asd JG 0120
echo>>seterror.asd SUB BL,30
echo>>seterror.asd MUL CX
echo>>seterror.asd ADD AX,BX
echo>>seterror.asd INC SI
echo>>seterror.asd JMP 010A
echo>>seterror.asd MOV AH,4C
echo>>seterror.asd INT 21
echo.>>seterror.asd
echo>>seterror.asd R CX
echo>>seterror.asd 24
echo>>seterror.asd W
echo>>seterror.asd Q

:debug
debug SETERROR.COM nul

:run
seterror.com %1
if not errorlevel %1 goto error
if not == if errorlevel %1 echo Current errorlevel is set.
if exist geterror.bat call geterror.bat %3
::if exist seterror.com del seterror.com
::if exist seterror.asd del seterror.asd
goto end

:error
echo.
echo Error: Fail to set current errorlevel.
echo.
goto end

:end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2004-03-02 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
This is Horst Schaeffer's assembly version. It also supports decimal parameters, but it's more organized than the one I wrote myself, and it also includes a command-line prompt. To use it, save the following text into the file el!.txt, then enter "debug < el!.txt" at the DOS command line to get the el!.com file. At runtime, the usage is "el! the errorlevel value you want to set".

A 100
MOV SI,0081 ;set the source string address to the command line
LODSB ;load one character at into register al
CMP AL,20 ;compare it with a space
JZ 0103 ;if equal to a space then jump to lodsb and load the next character
DEC SI ;otherwise decrement si by one, making the string pointer point to the first parameter
CMP AL,30 ;compare the loaded al value with character '0'
JB 0128 ;if less than '0' then jump to mov dx,0133 to display help
SUB AX,AX ;clear ax
SUB BH,BH ;clear bx
MOV DX,000A ;set dx to 10 (store the radix value)
MOV BL, ;take the character at into bl
INC SI ;increment string pointer si by one
SUB BL,30 ;subtract '0' (convert bl's character value into a binary number)
CMP BL,DL ;compare bl and dl (test whether bl is an invalid character ≥10)
JNB 0124 ;if bl ≥ 10 then jump to mov ah,4c (set errorlevel)
MUL DX ;multiply the original number ax by 10 (dx)
ADD AX,BX ;add the new number bx to the original number ax
JNB 0111 ;if cf=0 (??) then jump back to mov dx, 000a (convert the next character)
MOV AH,4C ;set errorlevel and exit
INT 21
MOV DX,0133 ;set pointer to the help string at
MOV AH,09 ;display the string at
INT 21
SUB AL,AL ;clear al
JMP 0124 ;jump back to mov dx,0133 to display help
DB 'EL! n' 0d 0a 'sets errorlevel to n=0..255' 0d 0a 24

r cx
58
n el!.com
w
q
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: