|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『楼 主』:
[分享]如何获取DOS环境中的ERRORLEVEL值
使用 LLM 解释/回答一下
To All:
以下批处理程序是关于获取DOS环境中由外部程序退出时产生的ERRORLEVEL值的若干算法。因为系统因素均未经测试,请有心者代劳,敬谢指正!
关于如何设置 ERRORLEVEL 值的程序,见:
http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9444
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
::geterror.bat - First version
::最中规中距,最美观大方的算法,也是最冗长繁复,最效率低下的算法
::我曾经在一个脚本增强工具包里见过,慨叹批处理程序也有这样的算法
::我是没耐心将它写全了相信各位明白我的意思了
@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
::最后一句中的IF NOT ERRORLEVEL 256其实不必要,不过要讲究形式美嘛<img src="images/smilies/face-laugh.png" align="absmiddle" border="0">
::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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
::geterror.bat - Second version
::一种五十步笑百步的改进版
::减少了一些IF NOT ERRORLEVEL 却增加了SET EL=
::曾经在帖子上有人这样贴过,倒是节约了些字节
::还是没写全,请众位有心的补齐吧<img src="images/smilies/face-wink.png" align="absmiddle" border="0">
@echo off
if errorlevel 0 set el=0
::上句中的if errorlevel 0 其实也用不着,之所以要写原因同上
if errorlevel 1 set el=1
if errorlevel 2 set el=2
......
if errorlevel 255 set el=255
::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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
::geterror.bat - Third version
::一个效率上有明显改进的算法,我自己最初的改进版
::利用了简单的数学原理,使经常获得的低errorlevel值有较好的执行时间
::可惜写起来还是太累,而且对于高errorlevel值效率还是太低
@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
::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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
=============
本文因重发问题被作者删除
=============
=============
This article was deleted by the author due to re - posting issues
=============
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
::geterror.bat - Forth version
::这是一个兼顾容量和效率的算法
::然而仍然存在高ERRORLEVEL值的慢获取,尤其不断地生成和销毁DOS壳更耗时间
@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
::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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-3 00:00 |
|
|
Wengier
系统支持
             “新DOS时代”站长
积分 27736
发帖 10521
注册 2002-10-9
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
自己编写批处理得到ERRORLEVEL值的方法我已经也曾做过,不过太复杂、麻烦。还是用COMMAND/Z或ERR2ENV等工具比较简便易行吧。
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和雨露,谢谢!)
 |
|
2004-2-3 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
::geterror.bat - Fifth version
::根据Roy发的同类程序修改而成,链接如下:
::http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?BoardID=12&ID=9348&replyID=56640&skin=1
::这是一个改进很大的算法,算法思想上由分区检测改为分位检测
::嵌套循环改为并列循环,时间代价下降了一个数量级
::壳的生成与销毁也被避免,即使对高ERRORLEVEL值的获取,也有了不错的性能
::篇幅也进一步缩减,这基本上已经是循环算法的最优算法了。
@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%
::未完待续
::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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-4 00:00 |
|
|
Kinglion
铂金会员
       痴迷DOS者
积分 5798
发帖 1924
注册 2003-6-20 来自 金獅電腦軟體工作室
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
我的批处理文件:
@echo off
for %%a in (0;1;2;3;4;5;6;7;8;9<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do if errorlevel %%a set testerr=%%a
for %%a in (10;11;12;13;14;15;16;17;18;19<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do if errorlevel %%a set testerr=%%a
for %%a in (20;21;22;23;24;25;26;27;28;29<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do if errorlevel %%a set testerr=%%a
for %%a in (30;31;32;33;34;35;36;37;38;39<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do if errorlevel %%a set testerr=%%a
for %%a in (40;41;42;43;44;45;46;47;48;49<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> 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=
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
|
|
2004-2-5 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
::geterror.bat - Sixth version
::所以说测试很重要,刚刚发现在fifth版中存在一个严重的bug:
::凡是大于等于200以上的errorlevel都会被测为299,这是修正后的版本
::请Kinglion兄和其他兄弟在测试一次吧
@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%
::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
::Please 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-2-6 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
下面是第七版程序,它包含了设置Errorlvel的代码,起因于此http://www.51ct.net/bbs/read.php?tid=4181&fpage=1 :: ErrLvl.bat - Reset/set and Display/get errorlevel (设置和获取错误返回码)
:: 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 (设置和获取错误返回码)
echo Usage(用法):
echo set errorlevel(设置错误返回码): errlvl decnum(十进制数)
echo get errorlevel(获取错误返回码): errlvl
echo @ 是可选的静默开关,使用它将不会显示执行结果
echo @ is optional quiet switch.
echo.
goto end
:Set - 设置错误返回码
if exist ~ErrLvl.com goto SetRun
:SetMake - 制作设置错误返回码的debug汇编脚本并生成com程序
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 - 运行设置错误返回码的com程序
~ErrLvl %1
if "%2"=="@" goto end
:Get - 获取错误返回码
set el=0
if errorlevel 1 set el=1
if not errorlevel 2 goto GetDisplay
:GetBit - 按百位、十位、个位的顺序获取错误返回码
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 - 显示错误返回码
if not "%1"=="@" echo Current errorlevel is(当前错误返回码为): %el%
goto end
:end
### 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-4-16 00:00 |
|