Re JonePeng:
我记得曾在 “手把手教你写批处理” 的编注中提到,从高到低的逆序并非是 errolevel 的必然要求,而只是 if errorlevel
num goto
lable 这种代码范式的习惯用法(也不是必须的)。
在这里,我特意强调上述代码以及其他许多初学者代码的失误是在于,未能充分理解 if errorlevel
value command arguments 这句代码的含义,它的意义是指:若当前的错误返回码大于等于指定的值
value ,就执行其后面的命令行
command arguments。
我未提到的是,这个 value 未必一定是个整数,它也可以是任意的 ASCII 字符或者其组合,比如 IF ERRORLEVEL ! 便等同于 IF ERRORLEVEL 241,这个特性与 if errorlevel 的命令行极为宽泛的解析算法相关 。
另外,此特性还存在许多扩展的用法,也牵涉到另外几个比较 errorlevel 的技巧,比如如果从 choice 接收了单个字母或数字,可以用以下语句简单判断,这种方法被称为 *BennyLevel Error Checking*。
@echo off
choice /n /cABCDEFGHIJKLMNOPQRSTUVWXYZ Input a letter:
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if errorlevel H%%d set letter=%%D
echo Your letter "%letter%"
这是大写字母的接受判断
@echo off
choice /n /cABCDEFGHIJKLMNOPQRSTUVWXYZ Input a letter:
for %%d in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if errorlevel x%%d set letter=%%D
echo Your letter "%letter%"
这是小写字母的接受判断,注意choice并未使用大小写敏感开关 /i ,所以它只是将输入转换为小写保存至变量,而非是接收小写字母。
@echo off
choice /n /c###0123456789 Input a number:
for %%d in (0 1 2 3 4 5 6 7 8 9) do if errorlevel J%%d set numer=%%D
echo Your number "%number%"
这是数字的接受判断
@echo off
choice /n /c#########0123456789 Input a number:
for %%d in (0 1 2 3 4 5 6 7 8 9) do if errorlevel 1%%d set numer=%%D
echo Your number "%number%"
这是另一个数字的接受判断,但是并未使用*BennyLevel Error Checking*。
手把手教你写批处理(willsort题注版)全文重贴
http://www.cn-dos.net/forum/viewthread.php?tid=13456&page=2#pid105799
Last edited by willsort on 2005-11-25 at 21:26 ]
Re JonePeng:
I remember that in the note of "Teaching You to Write Batch Processing Step by Step" , it was mentioned that the reverse order from high to low is not a necessary requirement of errorlevel, but just a customary usage (not mandatory) of the code pattern like if errorlevel
num goto
lable.
Here, I specifically emphasize that the mistakes in the above code and many other beginners' codes lie in the failure to fully understand the meaning of the code line if errorlevel
value command arguments, which means: if the current error return code is greater than or equal to the specified value
value, then execute the subsequent command line
command arguments.
What I didn't mention is that this value doesn't necessarily have to be an integer. It can also be any ASCII character or combination thereof. For example, IF ERRORLEVEL ! is equivalent to IF ERRORLEVEL 241. This feature is related to the extremely broad parsing algorithm of the if errorlevel command line.
In addition, there are many extended usages of this feature, and it also involves several other skills for comparing errorlevel. For example, if a single letter or number is received from choice, the following statement can be used for simple judgment. This method is called *BennyLevel Error Checking*.
@echo off
choice /n /cABCDEFGHIJKLMNOPQRSTUVWXYZ Input a letter:
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if errorlevel H%%d set letter=%%D
echo Your letter "%letter%"
This is the acceptance judgment for uppercase letters
@echo off
choice /n /cABCDEFGHIJKLMNOPQRSTUVWXYZ Input a letter:
for %%d in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if errorlevel x%%d set letter=%%D
echo Your letter "%letter%"
This is the acceptance judgment for lowercase letters. Note that choice did not use the case-insensitive switch /i, so it just converts the input to lowercase and saves it to the variable, not receiving lowercase letters.
@echo off
choice /n /c###0123456789 Input a number:
for %%d in (0 1 2 3 4 5 6 7 8 9) do if errorlevel J%%d set numer=%%D
echo Your number "%number%"
This is the acceptance judgment for numbers
@echo off
choice /n /c#########0123456789 Input a number:
for %%d in (0 1 2 3 4 5 6 7 8 9) do if errorlevel 1%%d set numer=%%D
echo Your number "%number%"
This is another acceptance judgment for numbers, but it does not use *BennyLevel Error Checking*.
Teaching You to Write Batch Processing (Willsort's Note Version) Full Posting Again
http://www.cn-dos.net/forum/viewthread.php?tid=13456&page=2#pid105799
Last edited by willsort on 2005-11-25 at 21:26 ]