Tip & How-To about Computers & Internet
The choice command from Win95 can't be used in WinXP or W2K you need to use the SET CHOICE = and the IF'%CHOICE%'== command here is the same example for Win95 example with the CHOICE and ERRORLEVEL commands.
Example One: (Win95)
In the below example a user would be prompted to enter an option of y,n, or Q to print you have selected YES , NO, or QUIT.
@ECHO OFF
:BEGIN
CLS
CHOICE /N /C:ynQ Select (y,n or Q)? %1
IF ERRORLEVEL ==3 GOTO QUIT
IF ERRORLEVEL ==2 GOTO NO
IF ERRORLEVEL ==1 GOTO YES
GOTO END
:QUIT
ECHO YOU HAVE PRESSED QUIT
GOTO END
:NO
ECHO YOU HAVE PRESSED NO
GOTO END
:YES
ECHO YOU HAVE PRESSED YES
:END
To add more choices increase the errorlevel number and append to the ynQ on the choice command
Example 2: (WinXP and W2K)
In the below example a user would be prompted to enter an option of y,n, or Q to print you have selected YES , NO, or QUIT.
@echo off
cls
:start
echo.
set choice=
set /p choice=Type (y,n or Q)?
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto yes
if '%choice%'=='n' goto no
if '%choice%'=='Q' goto quit
echo "%choice%" is not valid please try again
echo.
goto start
:yes
echo you have selected YES
goto end
:no
echo you have selected NO
goto end
:quit
echo you have selected QUIT
goto end
:end
To add more options simply add another if '%choice%'== goto line and give it a name and add the colon to the front of the name.
Hope this helps took me a whole night to work it out for Windows XP I like the way it works its much easier to understand when you don't have to use the ERRORLEVEL.
N.B> I have used 3 options Y,N or Q you don't have to and the choice I have kept simple you can copy this code and add your own replace the (you have selected YES) with whatever you require to happen if the user presses Y.
May 01, 2018 | Watches
Oct 18, 2016 | Computers & Internet
Jan 22, 2011 | MSI RS482M-IL (7145060RS482MIL)...
Jun 11, 2010 | Computers & Internet
1,126 people viewed this tip
Usually answered in minutes!
×