中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-02 02:29
48,037 topics / 350,122 posts / today 2 new / 48,250 members
DOS批处理 & 脚本技术(批处理室) » Batch File Tricks - Using the CHOICE Command for Time Delay
Printable Version  3,694 / 23
Floor1 Roy Posted 2003-08-16 00:00
管理员 Posts 1,633 Credits 4,869
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...
Floor2 iceboy Posted 2003-08-16 00:00
银牌会员 Posts 512 Credits 1,681
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
Floor3 qjiang Posted 2003-08-16 00:00
初级用户 Posts 1 Credits 106
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?
Floor4 xiaojun Posted 2003-08-16 00:00
银牌会员 Posts 499 Credits 2,202
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.


Floor5 hotdog Posted 2003-08-16 00:00
中级用户 Posts 78 Credits 411
Is ctty part of msdos?
Floor6 xiaojun Posted 2003-08-17 00:00
银牌会员 Posts 499 Credits 2,202
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?
Floor7 winddeity Posted 2003-08-21 00:00
初级用户 Posts 62 Credits 311
Is it an external command? Why doesn't my ms-dos7.1 have it?
Floor8 willsort Posted 2003-08-21 00:00
元老会员 Posts 1,512 Credits 4,432
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?
Floor9 下半辈子 Posted 2003-09-16 00:00
中级用户 Posts 23 Credits 204
Floor10 Roy Posted 2004-02-02 00:00
管理员 Posts 1,633 Credits 4,869
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...
Floor11 Kinglion Posted 2004-02-03 00:00
铂金会员 Posts 1,924 Credits 5,798 From 金獅電腦軟體工作室
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?
Floor12 Kinglion Posted 2004-02-03 00:00
铂金会员 Posts 1,924 Credits 5,798 From 金獅電腦軟體工作室
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
Floor13 xiaojun Posted 2004-02-03 00:00
银牌会员 Posts 499 Credits 2,202
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?





Floor14 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
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.


Floor15 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
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.


1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023