Computer Hope
Microsoft => Microsoft DOS => Topic started by: PirateSamir on January 23, 2009, 02:48:47 PM
-
How would i program it so that the only two possible answers are Y and N?
I know i have to use the if command for the second part, but how do i make it so that the only pressable buttons are Y and N or if i cant do that how do i make it so that if i press Y a certain thing happens, and if i press N a certain thing happens and if i press any other button then it will repeat the question.
Thnx in advance.
-
@ECHO OFF
:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
echo "I am here because you typed Y"
pause
exit
:somewhere_else
echo "I am here because you typed N"
pause
exit
-
@ECHO OFF
:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
echo "I am here because you typed Y"
pause
exit
:somewhere_else
echo "I am here because you typed N"
pause
exit
That code should work. Just remember to use the /I switch so it isn't case sensitive.
-
unfortunately that doesnt work. It doesn't matter what you press, it will go to the Y option every single time. :/
also could you explain to me the /P and the /I before choice.
Btw, im using Win XP and im not sure but i dont think that the choice command works.
-
Try again.. Copy the code again to a new file and try again.
I am surprised it doesn't work for you. Cause its a fairly straight forward code. It should work!!
As for your question:
/I ---- makes the character Case insensitive
/P ---- Prompt the user with the question that follows and set the answer to the variable.
-
ok it works this time, lol weird.
One more question. is there a way to represent all the characters? If so, can you do something like exclude certain characters while keeping the rest?
To make it more clear ill give you an example of what i want to do. I want a batch file that will ask you yes or no questions and the only buttons that will wield you a result would be Y and N, while the rest of the characters would just repeat the question. Is there a quicker way to program the rest of the characters to repeat your question?
Hope its clear to you what im saying.
-
Honestly not too sure what you mean.. Could you post an example.
The code in my previous post will only work if you type Y/N otherwise it will ask you the same question again and again.
-
OMG thank you! :D
This is exactly what i wanted :D
-
you could try CHOICE.EXE, the NT version of it. it has more features than Set /P
such as it automatically chooses for you if you dont specify what choice you want after so many seconds.
-
you could try CHOICE.EXE, the NT version of it. it has more features than Set /P
such as it automatically chooses for you if you dont specify what choice you want after so many seconds.
THis can be hard to find.
Open source choice.exe here:
http://winsupport.org/packages/choice.exe
-
As a code you can also use.
if /i "%variable%"=="string"
You can also put == instead of equ .
-
thanks guys, you really helped me.
-
Anytime ;)
-
While choice has more features then set /p. It is also more of a hassle. set /p is simple and easy. But yes, choice does have things such as waiting for a certain amount of time before giving a default answer.
-
http://hp.vector.co.jp/authors/VA007219/dkclonesup/choice.html
-
the NT version is the one that you would want.
-
3.1 or 3.5?
-
3.1 or 3.5?
Either, or NT4, or Win2K resource kit
-
k thnx, i got 3.5
gonna learn to use it now :D
-
k thnx, i got 3.5
gonna learn to use it now :D
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
-
ok, 2 questions
how would define your default choice?
and
what is errorlevel? (i've seen it quite oftenly in batch files) ???
-
ok, 2 questions
how would define your default choice?
and
what is errorlevel? (i've seen it quite oftenly in batch files) ???
Example
Default choice is Y, timeout is 10 seconds
choice /C:YN /T:Y,10 Press Y or N
echo errorlevel is: %errorlevel%
If Y was pressed (or timeout occurred) the %errorlevel% will be 1 (because Y is choice 1) and if N was pressed %errorlevel% will be 2 (because N is the second choice)
errorlevel is a system variable which is set by a program. Often 0=no error, 1 or more means an error. Choice uses it to pass the key pressed.
S:\>chtest1.bat
Press Y or N [Y,N]?Y
errorlevel is: 1
S:\>chtest1.bat
Press Y or N [Y,N]?N
errorlevel is: 2
-
so can errorlevel be used for anything else?
-
so can errorlevel be used for anything else?
Yes.
dir *.txt
if %errorlevel% GTR 0 echo "No text files"
Plenty of information if you use Google.
-
ok thnx :)
-
@ECHO OFF
:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
echo "I am here because you typed Y"
pause
exit
:somewhere_else
echo "I am here because you typed N"
pause
exit
Thanks hiteshsavla - Exactly what I required, saved me a lot of time. (I write batch files so infrequently these days that it takes time to get back into it)