Why you should use "choice", not "set /p" to accept user-input.
When you use set /p "var=Some user prompt"
then var
is set to the string entered from the keyboard.
- But note that if the user's response is simply Enter then
var
will remain unchanged
All fine in theory, if you can be certain that your users will never enter an unexpected string.
Suppose the user enters 2
to your first prompt
The if
statement would be interpreted as if "2" equ 2 (
Since "2"
and 2
are different, the if
statement will be evaluated as false. You need if "%choosecountry%" equ "2" (
So the code now becomes if "%choosecountry%" equ "2" (
Suppose the user enters United Kingdom
to your first prompt, instead of the expected 2
.
The if
statement would be interpreted as if "United Kingdom" equ "2" (
- well, that could be fixed with some more code.
But suppose the user enters UK
-yes. More code...
Suppose the entry was x"y
. This would lead to a syntax error - and there are oodles of other examples like %hello
which the user could enter but batch would object to.
Well - most, but probably not all of these unexpected input strings could probably be massaged to allow the cmd
processor to handle them smoothly, but that's a lot of unnecessary work and code.
The better solution is to use choice
which you use (but incorrectly, sorry) later in your code.
If you enter choice /?
from the prompt, you get a full description of choice
. In summary, you can use the command choice /C:YN
where YN
can be any string of unique alphamerics - a to z or a digit.
choice
waits for a single character and will beep if one of the choices set by the /c:
is not used.
If one of the allowed options is used, then it sets the magic variable errorlevel
to a value that depends on the position of the character within the allowed string, so pressing N
in response to choice /C:YN
would set errorlevel
to 2; for choice /C:NY
to 1
; for choice /C:ORANGES
to 4
.
magic variables are those that are set automatically like DATE
, TIME
, RANDOM
and others. These variables should not be set as part of a program as the value set
into the variable then overrides the value that the system sets.
magic variables otherwise work identically to normal user-variables, and ALL VARIABLES are strings. If the string is purely numeric then the set /a
command can be used to perform some mathematical operations. (there are some esoteric exceptions to the pure
-numeric string requirement - see set /?
from the prompt for details)
So, to structure a choice
for use from a menu, you might have
choice /C:YN
if "%errorlevel%" equ "255" goto savesettings
if "%errorlevel%" equ "2" goto continuetest
if "%errorlevel%" equ "1" goto createsavefiletest
Note that each side of the if
operator ("equ") must be quoted.
OR - you may have
choice /C:YN
if errorlevel 255 goto savesettings
if errorlevel 2 goto continuetest
if errorlevel 1 goto createsavefiletest
Note that these tests must be performed in reverse-numerical order as the meaning of if errorlevel n
is "if errorlevel is n or greater than n"
You could even use
choice /C:YN
goto point%errorlevel%
where :point1
is located at :createsavefiletest
, :point2
at :continuetest
and :point255
at :savesettings
.
Note that a label is simply a location in a file, so
some code...
:xyz
:abc
:hyg
some more code...
would mean that goto xyz
,goto abc
andgoto hyg
all do exectly the same thing.
This should enable you to solve your problem.
Batch is sensitive to spaces in an ordinary string SET
statement. SET FLAG = N
sets a variable named "FLAGSpace" to a value of "SpaceN". Remove Space from both sides of the =
.
Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign "
or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.