- 1 year ago
How to get the y/n key press directly without using choice and set /p
Comment
1 / 1
To get a simple y/n key press without using the choice
or set /p
commands in a Windows batch script, you can utilize the choice
command directly. Here's an example:
batch@echo off
echo Do you want to continue? (y/n)
choice /c yn > nul
if errorlevel 2 (
echo You chose NO.
) else (
echo You chose YES.
)
In the code above, the choice
command is used to prompt the user for a y/n input. The /c yn
option specifies the available choices as y
and n
. The > nul
is used to suppress the display of the selected choice.
The if errorlevel 2
condition is used to check if the user selected n
(errorlevel 2) and execute the corresponding code block. If the user selects y
, the else block will be executed.
This approach allows you to get a simple y/n key press directly in a batch script without using set /p
or additional commands.