标题: wait.com是怎样用的呀?
[打印本页]
作者: brightsm
时间: 2005-11-21 19:38
标题: wait.com是怎样用的呀?
wait.com是怎样用的呀?
里面的说明如下:
用法如下
WAIT [分:]秒[.10分之1秒] [提示语句]
最大时间: 1 小时; 默认时间: 无限
返回的Errorlevel值:
0: 超时
1..254: 按键的ASCII码(转为大写)
255: 非ASCII吗键
你的代码可写成
WAIT
if errorlevel 89 goto A 这个ASC是Y
if errorlevel 78 goto B 这个ASC是N
if errorlevel 67 goto C 这个ASC是C
IF ERRORLEVEL 11 GOTO B 这个ASC查出来是“起始位置”
但我写如下代码没有用呀!
wait 5 please input the number "1" or"2"or"3" rem 这儿停5秒 这儿这句正常执行
if errorlevel 49 goto 1 rem 49是1的ASC
if errorlevel 50 goto 2 rem 49是2的ASC
if errorlevel 51 goto 3 rem 49是3的ASC
if errorlevel 0 goto 1 rem 0超时,这儿作用是不按任何键则运行1代码段
if errorlevel 255 goto 1
:error
echo error ekede
goto end
:1
echo 1 is called
goto end
:2
echo 2 is called
goto end
:3
echo 1 is called
goto end
:end
不管按任何键,上面的代码只能运行代码1段,这是为什么?(包括2键和3键)
[
Last edited by brightsm on 2005-11-21 at 19:43 ]
作者: brightsm
时间: 2005-11-22 16:28
标题: 算了,还是用choice吧!
算了,还是用choice吧!
我最终选择了choice来完成批处理了!
作者: JonePeng
时间: 2005-11-22 19:35
楼主想到用WAIT命令来取键盘输入值是很好的想法,但不必这样舍近求远、使用CHOICE。楼主的批处理失败的原因是忽视了errorlevel值必须
从高至低来排列这一点,所以 if errorlevel 应该写成这样:
if errorlevel 255 goto null
if errorlevel 51 goto 3
if errorlevel 50 goto 2
if errorlevel 49 goto 1
if errorlevel 0 goto 1
我把楼主的测试程序修改成如下
@echo off
wait 5 Please input the number "1" or "2" or "3"
if errorlevel 255 goto error
if errorlevel 51 goto 3
if errorlevel 50 goto 2
if errorlevel 49 goto 1
if errorlevel 0 goto null
:error
echo Error occured!
rem 楼主的“ekede”既不像拼音也不像日文的罗马拼音,我猜应该是Occured
goto end
:1
echo 1 is called
goto end
:2
echo 2 is called
goto end
:3
echo 3 is called
goto end
:null
echo Nothing happened :p
:end
[
Last edited by JonePeng on 2005-11-22 at 19:38 ]
作者: willsort
时间: 2005-11-25 21:00
Re JonePeng:
我记得曾在 “手把手教你写批处理”[1] 的编注中提到,从高到低的逆序并非是 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*。
[1] 手把手教你写批处理(willsort题注版)全文重贴
http://www.cn-dos.net/forum/view ... mp;page=2#pid105799
[
Last edited by willsort on 2005-11-25 at 21:26 ]
作者: JonePeng
时间: 2005-11-25 21:21
感谢willsort版主的提醒和关注!
“手把手”的那个帖子我一年多前看过,现在又更新了那么多精彩的总结,感激不尽!