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-02 01:30
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch File Tricks - Using the CHOICE Command for Time Delay View 3,688 Replies 23
Original Poster Posted 2003-08-16 00:00 ·  中国 香港 有线电视
管理员
★★★★
專業島民
Credits 4,869
Posts 1,633
Joined 2002-12-10 00:00
23-year member
UID 465
Gender Male
Status Offline
Want to delay for a bit in a batch file, but only want to use basic DOS commands—what to do?
There is a way!
Just use the choice command!!
For example:
A 10-second delay, press q to quit,
choice /c:!q /t:!,10 /n Press q to exit...

Ultimate secret technique!! Force a 10-second delay!!
type nul|choice /c:! /t:!,10 /n Please wait for 10 seconds...
我的網站:http://mw16.2ya.com/ 我的網誌: http://scrappedblog.blogspot.com/
~
我的Winamp正在播放的歌曲:
Floor 2 Posted 2003-08-16 00:00 ·  中国 湖北 武汉 联通
银牌会员
★★★
Credits 1,681
Posts 512
Joined 2003-08-02 00:00
23-year member
UID 7953
Gender Male
Status Offline
If you put ctty nul before the delay, and ctty con after it, then pressing CTRL+C during the delay will crash the system.. hehe
Somehow somewhere I've got to choose.
No matter if it is win or lose.
Floor 3 Posted 2003-08-16 00:00 ·  中国 江苏 盐城 滨海县 电信
初级用户
Credits 106
Posts 1
Joined 2003-08-16 00:00
22-year member
UID 8716
Gender Male
Status Offline
In choice /c:!q /t:!,10 /n Press q to exit..., what does c:!q mean? And in /t:!,10, what does !, mean? Could you explain this part?
Floor 4 Posted 2003-08-16 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
So after putting this command at the start of the batch file, if I regret it in the first 10 seconds after running the bat file, pressing q will stop it from running; is that right? Thanks.


一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 5 Posted 2003-08-16 00:00 ·  中国 浙江 杭州 余杭区 电信
中级用户
★★
Credits 411
Posts 78
Joined 2003-07-27 00:00
23-year member
UID 7622
Gender Male
Status Offline
Is ctty part of msdos?
Floor 6 Posted 2003-08-17 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
The following is quoted from xiaojun at 2003-8-16 21:31:46:
So after putting this command at the start of the batch file, if I regret it in the first 10 seconds after running the bat file, pressing q will stop it from running; is that right? Thanks.





Which expert: is that what it means?
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 7 Posted 2003-08-21 00:00 ·  中国 安徽 合肥 中国科学技术大学教育网
初级用户
★★
Credits 311
Posts 62
Joined 2003-08-16 00:00
22-year member
UID 8721
Gender Male
Status Offline
Is it an external command? Why doesn't my ms-dos7.1 have it?
一切都会好起来的
Floor 8 Posted 2003-08-21 00:00 ·  中国 山东 烟台 福山区 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re roy:

I've seen this method in a foreign-language book, but to this day I still can't solve a flaw mentioned in the book, namely: after pressing other keys, the countdown stops. I wonder if Roy has any solution?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2003-09-16 00:00 ·  中国 广东 汕头 潮阳区 电信
中级用户
Credits 204
Posts 23
Joined 2003-09-16 00:00
22-year member
UID 9875
Gender Male
Status Offline
Floor 10 Posted 2004-02-02 00:00 ·  中国 香港
管理员
★★★★
專業島民
Credits 4,869
Posts 1,633
Joined 2002-12-10 00:00
23-year member
UID 465
Gender Male
Status Offline
The following is quoted from willsort at 2003-8-21 16:21:44:
Re roy:

    I've seen this method in a foreign-language book, but to this day I still can't solve a flaw mentioned in the book, namely: after pressing other keys, the countdown stops. I wonder if Roy has any solution?
Use the forced one...
我的網站:http://mw16.2ya.com/ 我的網誌: http://scrappedblog.blogspot.com/
~
我的Winamp正在播放的歌曲:
Floor 11 Posted 2004-02-03 00:00 ·  中国 辽宁 沈阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
The following is quoted from hotdog at 2003-8-16 22:51:26:
Is ctty part of msdos?


CTTY is an internal DOS command that changes the terminal device used to control the computer.

If you want to use another device for command input, you can do it with the CTTY command.

Syntax

CTTY device

Parameter

device
Specifies the alternate device used for MS-DOS command input. Valid values for
parameter device are:
PRN, LPT1, LPT2, LPT3, CON, AUX, COM1, COM2, COM3 and COM4.

Related command

The input device can also be changed when specifying a command interpreter; for details,
see the command.


CTTY—Notes

Setting a serial port for CTTY

Before using the CTTY command, you can first use the MODE command to set the serial
port's baud rate, parity bit, data bits, and stop bits.

Programs that do not use MS-DOS and CTTY

Many programs do not use MS-DOS to manage input or output. These programs send input
directly to the computer's hardware. The CTTY command has no effect on these programs. It
only affects programs that use MS-DOS to read input and display output.

Using COMMAND to set the terminal device

Besides the CTTY command, you can also use the device parameter of the COMMAND
command to specify the input device.


Understand now?
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 12 Posted 2004-02-03 00:00 ·  中国 辽宁 沈阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
About the CHOICE command

In a batch program, it prompts the user to make a choice. It displays a specified
prompt and pauses, waiting for the user to choose from a specified set of keys. This
command can only be used in batch programs.

For a detailed explanation of the ERRORLEVEL parameter, see the and commands.

Syntax

CHOICE keys] c,nn]

Parameter

text
Specifies the text to display before the prompt. Double quotation marks are needed
only when the text before the prompt includes the switch character
(/) as part of the text. If no text is specified, CHOICE only dis-
plays a prompt.

Switches

/Ckeys
Specifies the keys accepted in the prompt. When displayed, these keys appear inside
, separated by commas, followed by a question mark. If the /C switch is not
specified, CHOICE uses the default value YN. The colon ( is optional.

/N
Makes CHOICE not display the prompt, but the text before the prompt is still shown.
If the /N switch is specified, the specified keys are still valid.

/S
Makes CHOICE case-sensitive. If /S is not specified, CHOICE does not distinguish
between uppercase and lowercase for keys entered by the user.

/Tc,nn
Before using a specified key as the default choice, CHOICE waits for a specified
number of seconds. The values in the /T switch are as follows:

c
Specifies the character to use as the default after waiting nn seconds. This
character must appear in the set of keys after the /C switch.
nn
Specifies the pause time in seconds. Acceptable values are from 0 to 99. If
set to 0, there is no pause before using the default.

CHOICE—Examples

To let the user see prompt information in a batch file, you can use the CHOICE
command in the batch file.
For example, if the following syntax is used in a batch file:

choice /c:ync

then when CHOICE runs, the user will see:

?

Adding text to the above example:

choice /c:ync Yes, No, or Continue

choice /c:ync Yes, No, or Continue

When CHOICE starts, the user sees:

Yes, No, or Continue ?

What the user sees after removing the prompt

As in the following example, use the /N switch in a batch program to remove the prompt:

choice /n Yes, No, or Continue?

When CHOICE executes, the user sees only the specified text:

Yes, No, or Continue?

What the user sees after using the T switch

If the following statement is used in a batch program:

choice /c:ync /t:n,5

When CHOICE executes, the user will see:

?

After 5 seconds, if the user has not pressed any key, CHOICE selects N and returns
an ERRORLEVEL value of 2. If a key is pressed within 5 seconds, CHOICE returns the value
corresponding to the user's choice.

When starting the computer, if you want the user to choose whether to defragment
drive C, add the following lines to the AUTOEXEC.BAT file:

choice Defrag drive /ty,5
if errorlevel 2 goto SkipDefrag
defrag c:
:SkipDefrag

If N is pressed within 5 seconds, DEFRAG will not run, and CHOICE returns an
ERRORLEVEL value of 2. If N is not pressed within 5 seconds, or Y is chosen, DEFRAG will
run on drive C.

Using CHOICE in a batch program

The following batch program demonstrates how to use CHOICE options to let the user
choose one of three programs to run:
MS-DOS Editor, Microsoft Anti-virus, or Microsoft Backup.

Note that the IF ERRORLEVEL statements here are listed in descending order. If the
value of the ERRORLEVEL parameter returned by CHOICE is greater than or equal to the
parameter specified by the IF command, MS-DOS treats the IF statement as true.

@echo off
cls
echo.
echo A Microsoft Editor
echo B Microsoft Anti-Virus
echo C Microsoft Backup
echo.
choice /c:abc Choose an option
if errorlevel 3 goto MSBackup
if errorlevel 2 goto Msav
if errorlevel 1 goto Edit

:Edit
edit
goto End

:Msav
msav
goto End

:Msbackup
msbackup
goto End

:End
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 13 Posted 2004-02-03 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Question: in a batch file, to keep it from displaying anything, I added ctty nul, so why are there still error prompts?
This part: "choice Defrag drive /ty,5
if errorlevel 2 goto SkipDefrag
defrag c:
:SkipDefrag "
/ty can't be written together, right? Or... can it be written like that?





一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 14 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re roy:

What I mean is to find a way to block only invalid keys, rather than the effect of all keys on the countdown; I'd even be willing to use DEBUG to modify a copy of CHOICE if necessary.


※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

ctty has already been explained above
Programs that do not use MS-DOS and CTTY

Many programs do not use MS-DOS to manage input or output. These programs send input
directly to the computer's hardware. The CTTY command has no effect on these programs. It
only affects programs that use MS-DOS to read input and display output.

ctty nul hands the system's input/output over to the null device, and can block all output managed by MS-DOS, including error messages. Could you explain your situation in more detail?

choice uses Y and N as the default response options, so the usage above is correct.

KINGLION's text is taken from the MSDOS documentation, and is quite organized and authoritative; you can learn a lot from it.


※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: