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-06-24 06:11
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » [Share] How to Obtain the ERRORLEVEL Value in a DOS Environment View 2,693 Replies 10
Original Poster Posted 2004-02-03 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 following batch programs are about several algorithms for obtaining the ERRORLEVEL value generated when an external program exits in the DOS environment. Since the system factors have not been tested, please have the interested person do it on your behalf, and thank you for your correction!

For the program on how to set the ERRORLEVEL value, see:
http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9444







※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::geterror.bat - First version
::The most conventional and most beautiful algorithm, also the most lengthy and most inefficient algorithm
::I once saw it in a script enhancement tool kit, sighing that batch processing programs also have such algorithms
::I don't have the patience to write it all out, I believe you understand what I mean
@echo off
if errorlevel 0 if not errorlevel 1 set el=1
if errorlevel 1 if not errorlevel 2 set el=2
...........
if errorlevel 255 if not errorlevel 256 set el=255
::The last IF NOT ERRORLEVEL 256 in the sentence is actually unnecessary, but it's for the sake of formal beauty
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::geterror.bat - Second version
::A second-rate improvement
::Reduced some IF NOT ERRORLEVEL but added SET EL=
::Someone once posted like this on the forum, which actually saves some bytes
::Still not fully written, please fill it in if you have the heart ;P
@echo off
if errorlevel 0 set el=0
::The above "if errorlevel 0" is actually unnecessary, and the reason is the same as above
if errorlevel 1 set el=1
if errorlevel 2 set el=2
......
if errorlevel 255 set el=255
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::geterror.bat - Third version
::An algorithm with obvious improvement in efficiency, my initial improved version
::Using simple mathematical principles to make the frequently obtained low errorlevel values have better execution time
::Unfortunately, it's still too tiring to write, and the efficiency is still too low for high errorlevel values
@echo off
if not errorlevel 1 goto two0
if not errorlevel 2 goto two1
if not errorlevel 4 goto two2
if not errorlevel 8 goto two3
if not errorlevel 16 goto two4
if not errorlevel 32 goto two5
if not errorlevel 64 goto two6
if not errorlevel 128 goto two7
if not errorlevel 256 goto two8

:two0
set el=0
goto end

:two1
set el=1
goto end

:two2
if errorlevel 2 set el=2
if errorlevel 3 set el=3
goto end

:two3
if errorlevel 4 set el=4
if errorlevel 5 set el=5
...
goto end

:two4
...

......
if errorlevel 255 set el=255
goto end

:end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2004-02-03 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 article was deleted by the author due to re - posting issues
=============
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::geterror.bat - Forth version
::This is an algorithm that balances capacity and efficiency
::However, there is still slow acquisition with high ERRORLEVEL values, especially continuously generating and destroying DOS shells is more time-consuming
@echo off
set el=0
if not errorlevel 1 goto end
if not errorlevel 10 goto ten0
if not errorlevel 100 goto ten1
goto ten2

:ten0
for %%n in (1 2 3 4 5 6 7 8 9) do if errorlevel %%n set el=%%n
goto end

:ten1
for %%n in (1 2 3 4 5 6 7 8 9) do %comspec% /c for %%m in (1 2 3 4 5 6 7 8 9) do if errorlevel %%n%%m set el=%%n%%m
goto end

:ten2
for %%n in (1 2 3 4 5 6 7 8 9) do %comspec% /c for %%m in (1 2 3 4 5 6 7 8 9) do if errorlevel 1%%n%%m set el=1%%n%%m
for %%n in (1 2 3 4 5) do %comspec% /c for %%m in (1 2 3 4 5 6 7 8 9) do if errorlevel 2%%n%%m set el=2%%n%%m
goto end

:end



※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2004-02-03 00:00 ·  加拿大 安大略省 多伦多
系统支持
★★★★★★
“新DOS时代”站长
Credits 27,736
Posts 10,521
Joined 2002-10-09 12:00
23-year member
UID 9
Status Offline
I have also tried the method of getting the ERRORLEVEL value by writing batch processing myself, but it is too complicated and troublesome. It is still easier and more feasible to use tools like COMMAND/Z or ERR2ENV.
Wengier - 新DOS时代

欢迎大家来到我的“新DOS时代”网站,里面有各类DOS软件和资料,地址:
http://wendos.mycool.net/

E-Mail & MSN: wengierwu AT hotmail.com (最近比较忙,有事请联系DOSroot和雨露,谢谢!)

Floor 8 Posted 2004-02-04 00:00 ·  中国 山西 太原 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
::geterror.bat - Fifth version
::Modified based on a similar program posted by Roy, the link is as follows:
::http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?BoardID=12&ID=9348&replyID=56640&skin=1
::This is a greatly improved algorithm. The algorithm idea changes from partition detection to digit detection.
::Nested loops are changed to parallel loops, and the time cost is reduced by an order of magnitude.
::The generation and destruction of the shell are also avoided. Even for obtaining high ERRORLEVEL values, it has good performance.
::The length is further reduced. This is basically the optimal algorithm for the loop algorithm.
@echo off
set el=0
if errorlevel 1 set el=1
if not errorlevel 2 goto end

:partbydec
for %%h in (1 2) do if errorlevel %%h00 set elh=%%h
if errorlevel 10 for %%t in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elh%%%t0 set elt=%elh%%%t
for %%o in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elt%%%o set el=%elt%%%o
set elh=
set elt=

:end
if not == echo Current errorlevel is %el%










※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2004-02-05 00:00 ·  中国 辽宁 沈阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
My batch file:
@echo off
for %%a in (0;1;2;3;4;5;6;7;8;9 do if errorlevel %%a set testerr=%%a
for %%a in (10;11;12;13;14;15;16;17;18;19 do if errorlevel %%a set testerr=%%a
for %%a in (20;21;22;23;24;25;26;27;28;29 do if errorlevel %%a set testerr=%%a
for %%a in (30;31;32;33;34;35;36;37;38;39 do if errorlevel %%a set testerr=%%a
for %%a in (40;41;42;43;44;45;46;47;48;49 do if errorlevel %%a set testerr=%%a
.
.
.
for %%a in (250;251;252;253;254;255) do if errorlevel %%a set testerr=%%a
echo.
echo Current errorlevel is : %testerr%
echo.
set testerr=


熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 10 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
::geterror.bat - Sixth version
::So testing is very important. Just found that there is a serious bug in the fifth version:
::Any errorlevel greater than or equal to 200 will be tested as 299. This is the revised version
:lease Brother Kinglion and other brothers test again
@echo off
set el=0
if errorlevel 1 set el=1
if not errorlevel 2 goto end

:partbydec
for %%h in (1 2) do if errorlevel %%h00 set elh=%%h
if errorlevel 10 if not == for %%t in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elh%%%t0 set elt=%elh%%%t
if == for %%t in (0 1 2 3 4 5) do if errorlevel %elh%%%t0 set elt=%elh%%%t
if not == for %%o in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elt%%%o set el=%elt%%%o
if == for %%o in (0 1 2 3 4 5) do if errorlevel %elt%%%o set el=%elt%%%o
set elh=
set elt=

:end
if not == echo Current errorlevel is %el%





※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2005-04-16 00:00 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
### ErrLvl.bat - Reset/set and Display/get errorlevel (Set and get error return code)
:: Will Sort - 2005/04/16 - Win98/MSDOS7.1/CMD@WinXP
:: Test:
:: if %1>255 ==> %el%=(%1 mod 256)
:: if %1=e11 ==> %el%=0
:: if %1=11e ==> %el%=11
@echo off
if "%1"=="" goto Get
if "%1"=="@" goto Get
if not "%1"=="/?" goto Set

:help
echo.
echo ErrLvl.bat - Reset/set and Display/get errorlevel (Set and get error return code)
echo Usage:
echo set errorlevel: errlvl decnum (decimal number)
echo get errorlevel: errlvl
echo @ is an optional silent switch. Using it will not display the execution result
echo @ 是可选的静默开关,使用它将不会显示执行结果
echo.
goto end

:Set - Set error return code
if exist ~ErrLvl.com goto SetRun
:SetMake - Make debug assembly script to set error return code and generate com program
echo >~ErrLvl.asd A100
echo>>~ErrLvl.asd MOV SI,0082
echo>>~ErrLvl.asd XOR AX,AX
echo>>~ErrLvl.asd XOR BX,BX
echo>>~ErrLvl.asd MOV CX,000A
echo>>~ErrLvl.asd MOV BL,
echo>>~ErrLvl.asd CMP BL,30
echo>>~ErrLvl.asd JL 0120
echo>>~ErrLvl.asd CMP BL,39
echo>>~ErrLvl.asd JG 0120
echo>>~ErrLvl.asd SUB BL,30
echo>>~ErrLvl.asd MUL CX
echo>>~ErrLvl.asd ADD AX,BX
echo>>~ErrLvl.asd INC SI
echo>>~ErrLvl.asd JMP 010A
echo>>~ErrLvl.asd MOV AH,4C
echo>>~ErrLvl.asd INT 21
echo.>>~ErrLvl.asd
echo>>~ErrLvl.asd N ~ErrLvl.com
echo>>~ErrLvl.asd R CX
echo>>~ErrLvl.asd 24
echo>>~ErrLvl.asd W
echo>>~ErrLvl.asd Q
debug < ~ErrLvl.asd > nul

:SetRun - Run the com program to set error return code
~ErrLvl %1
if "%2"=="@" goto end

:Get - Get error return code
set el=0
if errorlevel 1 set el=1
if not errorlevel 2 goto GetDisplay

:GetBit - Get error return code by hundreds, tens, and units digit order
set el=
if errorlevel 100 for %%n in (1 2) do if errorlevel %%n00 set el=%%n
set #el=0 1 2 3 4 5 6 7 8 9
if "%el%"=="2" set #el=0 1 2 3 4 5
if errorlevel 10 for %%n in (%#el%) do if errorlevel %el%%%n0 set el=%el%%%n
set #el=0 1 2 3 4 5 6 7 8 9
if "%el%"=="25" set #el=0 1 2 3 4 5
for %%n in (%#el%) do if errorlevel %el%%%n set el=%el%%%n
set #el=

:GetDisplay - Display error return code
if not "%1"=="@" echo Current errorlevel is: %el%
goto end

:end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: