Computer Hope

Microsoft => Microsoft DOS => Topic started by: PirateSamir on January 23, 2009, 02:48:47 PM

Title: Making [Y/N] questions in batch files
Post 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.
Title: Re: Making [Y/N] questions in batch files
Post by: hiteshsavla on January 23, 2009, 03:16:25 PM
Code: [Select]
@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
Title: Re: Making [Y/N] questions in batch files
Post by: Helpmeh on January 23, 2009, 03:21:18 PM
Code: [Select]
@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.
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 23, 2009, 03:30:06 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: hiteshsavla on January 23, 2009, 03:35:12 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 23, 2009, 03:52:36 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: hiteshsavla on January 23, 2009, 03:56:05 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 23, 2009, 03:59:22 PM
OMG thank you! :D
This is exactly what i wanted :D
Title: Re: Making [Y/N] questions in batch files
Post by: macdad- on January 23, 2009, 04:10:05 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: Dias de verano on January 24, 2009, 01:12:33 AM
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
Title: Re: Making [Y/N] questions in batch files
Post by: BatchFileCommand on January 24, 2009, 07:13:35 AM
As a code you can also use.

Code: [Select]
if /i "%variable%"=="string"
You can also put == instead of equ .
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 24, 2009, 08:58:35 AM
thanks guys, you really helped me.
Title: Re: Making [Y/N] questions in batch files
Post by: macdad- on January 24, 2009, 11:19:57 AM
Anytime  ;)
Title: Re: Making [Y/N] questions in batch files
Post by: BatchFileCommand on January 25, 2009, 02:17:53 PM
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.
Title: Re: Making [Y/N] questions in batch files
Post by: WillyW on January 25, 2009, 05:01:30 PM
http://hp.vector.co.jp/authors/VA007219/dkclonesup/choice.html
Title: Re: Making [Y/N] questions in batch files
Post by: macdad- on January 25, 2009, 06:22:57 PM
the NT version is the one that you would want.
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 26, 2009, 10:57:24 AM
3.1 or 3.5?
Title: Re: Making [Y/N] questions in batch files
Post by: Dias de verano on January 26, 2009, 11:23:47 AM
3.1 or 3.5?

Either, or NT4, or Win2K resource kit
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 26, 2009, 11:38:43 AM
k thnx, i got 3.5
gonna learn to use it now :D
Title: Re: Making [Y/N] questions in batch files
Post by: Dias de verano on January 26, 2009, 11:43:35 AM
k thnx, i got 3.5
gonna learn to use it now :D
Code: [Select]
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.
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 26, 2009, 11:55:00 AM
ok, 2 questions
how would define your default choice?
and
what is errorlevel? (i've seen it quite oftenly in batch files)  ???
Title: Re: Making [Y/N] questions in batch files
Post by: Dias de verano on January 26, 2009, 12:44:09 PM
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

Code: [Select]
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.

Code: [Select]
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
Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 26, 2009, 12:49:58 PM
so can errorlevel be used for anything else?
Title: Re: Making [Y/N] questions in batch files
Post by: Dias de verano on January 26, 2009, 12:59:32 PM
so can errorlevel be used for anything else?

Yes.

Code: [Select]
dir *.txt
if %errorlevel% GTR 0 echo "No text files"

Plenty of information if you use Google.

Title: Re: Making [Y/N] questions in batch files
Post by: PirateSamir on January 26, 2009, 01:03:35 PM
ok thnx :)
Title: Re: Making [Y/N] questions in batch files
Post by: ht on January 08, 2011, 03:20:54 PM
Code: [Select]
@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)