tags:

views:

51

answers:

2

I have written the following code

choice /m "Do you want to add another profile" 
if errorlevel 1 set /p profile_name1=Enter one of the above profile names:

However it always runs "Enter one of the above profile names:" even though I pressed no. What did i do wrong?

Thanks!

A: 

First, /m is not a valid argument you can pass to choice. Try removing that, it might be confusing choice.

Also: IF ERRORLEVEL returns TRUE if the return code was equal to or higher than the specified errorlevel. Try checking every possible return value (in your case, both yes and no)

IF ERRORLEVEL 2 SET ANS="No"
IF ERRORLEVEL 1 SET ANS="Yes"

and use the value of ANSwer in the rest of your batch file.

Jan
Wrong, according to the result of choice /?: /M text Specifies the message to be displayed before the prompt. If not specified, the utility displays only a prompt.
Jonno_FTW
Jan
I have a basic windows XP install - I don't even have choice on my machine, so I only have online documentation to go by as far as that goes, but the real problem is the errorlevel line anyways.
Jan
@Jan: in newer versions of Windows, the choice command has different switches, including /M. For Win 7 for example, you get: `CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]`
JRL
I am using Win 7: http://i34.tinypic.com/1085xlz.png
Jonno_FTW
haha jonno FTW lol
thegreyspot
+1  A: 

You need to give instructions for each outcome. E.g.:

choice /m "Do you want to add another profile"
if errorlevel 2 goto :doOtherStuff
if errorlevel 1 set /p profile_name1=Enter one of the above profile names: 
:doOtherStuff

Also note that the order is important, you must list the errorlevels in descending order (2 then 1).

JRL
Thank you sir, you have saved me :)
thegreyspot