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-08-01 13:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How is wait.com used? View 1,805 Replies 4
Original Poster Posted 2005-11-21 19:38 ·  中国 新疆 巴音郭楞蒙古自治州 中移铁通
新手上路
Credits 8
Posts 2
Joined 2005-11-21 14:46
20-year member
UID 45663
Status Offline
How to use wait.com?

The instructions are as follows:

Usage is as follows

WAIT seconds
Maximum time: 1 hour; Default time: infinite
Returned Errorlevel value:
0: Timeout
1..254: ASCII code of the key pressed (converted to uppercase)
255: Non-ASCII key

Your code can be written as
WAIT
if errorlevel 89 goto A This ASC is Y
if errorlevel 78 goto B This ASC is N
if errorlevel 67 goto C This ASC is C
IF ERRORLEVEL 11 GOTO B This ASC is found to be "start position"

But the following code I wrote doesn't work!

wait 5 please input the number "1" or"2"or"3" rem Stop here for 5 seconds This line executes normally
if errorlevel 49 goto 1 rem 49 is the ASC of 1
if errorlevel 50 goto 2 rem 49 is the ASC of 2
if errorlevel 51 goto 3 rem 49 is the ASC of 3
if errorlevel 0 goto 1 rem 0 is timeout, here the function is to run the code segment 1 if no key is pressed
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

No matter which key is pressed, the above code can only run code segment 1. Why is this? (Including keys 2 and 3)

[ Last edited by brightsm on 2005-11-21 at 19:43 ]
Floor 2 Posted 2005-11-22 16:28 ·  中国 新疆 巴音郭楞蒙古自治州 中移铁通
新手上路
Credits 8
Posts 2
Joined 2005-11-21 14:46
20-year member
UID 45663
Status Offline
Well, let's just use choice!
I finally chose choice to complete the batch processing!
Floor 3 Posted 2005-11-22 19:35 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
The building owner's idea of using the WAIT command to get the keyboard input value is a good one, but there's no need to go the long way around and use CHOICE. The reason the building owner's batch processing failed is that he neglected the point that the errorlevel values must be arranged from high to low. So if errorlevel should be written like this:

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

I modified the building owner's test program to the following

@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 The building owner's "ekede" is neither like pinyin nor like the Romanization of Japanese, I guess it's 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 ]
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 4 Posted 2005-11-25 21:00 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2005-11-25 21:21 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Thanks to the reminder and attention from moderator willsort!
I read the "step-by-step" post more than a year ago, and now there are so many wonderful summaries updated again, I'm extremely grateful!
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Forum Jump: