thegreyspot
thegreyspot

Reputation: 4109

Choice command not differing

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!

Upvotes: 0

Views: 259

Answers (2)

user196056
user196056

Reputation:

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.

Upvotes: 0

JRL
JRL

Reputation: 77995

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).

Upvotes: 2

Related Questions