set /p choice /t prompt? correct format

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
crobertson
Posts: 20
Joined: 25 May 2012 12:34

set /p choice /t prompt? correct format

#1 Post by crobertson » 01 Feb 2015 14:38

I haven't found a good choice example on the web. I had to format it like this. Can anyone get the message in line with the choice option;

echo Would you like to set folders to exclude?
CHOICE /T 4 /C ync /CS /D n
if errorlevel=3 goto variables
if errorlevel=2 goto backup
if errorlevel=1 goto exclude
echo Something is wrong

I also found selecting the last option "c", errorlevel is both 1,2&3 (sigh)
I would like to use set /p choice.... like;
set /p userinp=Choose a number (1-6):
or
set /P c=Did you run as administrator? Y/N?....

I appreciate your help.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: set /p choice /t prompt? correct format

#2 Post by Compo » 01 Feb 2015 15:12

Try without the =

Code: Select all

Choice /T 4 /C ync /CS /D n /N /M "Press y for Yes, n for No or c for Cancel."
If ErrorLevel 3 GoTo variables
If ErrorLevel 2 GoTo backup
If ErrorLevel 1 GoTo exclude
Echo(Something is wrong

crobertson
Posts: 20
Joined: 25 May 2012 12:34

Re: set /p choice /t prompt? correct format

#3 Post by crobertson » 01 Feb 2015 15:58

Thank you, that was simple enough.

Is there a way to use the Set /p so I can test Y or N instead of errorlevel?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: set /p choice /t prompt? correct format

#4 Post by Compo » 01 Feb 2015 16:53

You test for the errorlevel using Choice

Code: Select all

Choice /T 10 /C YN /D N /N /M "Press Y for Yes or N for No."
If ErrorLevel 2 GoTo ChoseN
Echo( Yes was chosen.
GoTo EndMe
:ChoseN
Echo( No was chosen.
:EndMe
Pause

To use Set

Code: Select all

@Echo Off
SetLocal
:Loop
Cls
Set "Opt="
Set/P "Opt=Press Y for Yes or N for No. "
If /I "%Opt%" NEq "N" (If /I "%Opt%" NEq "Y" GoTo :Loop) Else (GoTo ChoseN)
Echo( Yes was chosen.
GoTo EndMe
:ChoseN
Echo( No was chosen.
:EndMe
Pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: set /p choice /t prompt? correct format

#5 Post by Squashman » 01 Feb 2015 17:52

IF command requires two equal signs for string comparisons.

Post Reply