中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-13 22:14
47,812 topics / 349,916 posts / today 0 new / 48,268 members
DOS开发编程 & 发展交流 (开发室) » [Share] How to Obtain the ERRORLEVEL Value in a DOS Environment
Printable Version  2,888 / 10
Floor1 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
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







Floor2 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
::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
Floor3 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
::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
Floor4 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
::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
Floor5 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
=============
This article was deleted by the author due to re - posting issues
=============
Floor6 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
::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



Floor7 Wengier Posted 2004-02-03 00:00
系统支持 Posts 10,521 Credits 27,736
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.
Floor8 willsort Posted 2004-02-04 00:00
元老会员 Posts 1,512 Credits 4,432
::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%










Floor9 Kinglion Posted 2004-02-05 00:00
铂金会员 Posts 1,924 Credits 5,798 From 金獅電腦軟體工作室
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=


Floor10 willsort Posted 2004-02-06 00:00
元老会员 Posts 1,512 Credits 4,432
::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%





Floor11 willsort Posted 2005-04-16 00:00
元老会员 Posts 1,512 Credits 4,432
### 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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023