Hmm... well, TBH, you wouldn't need to ask the question if you did succeed...
So - to solve your problem, we'd need to know a little more The first is about how your file starts. The normal start to a batch file is
@echo off
setlocal
which turns default command-echoing off to not clutter the screen and then establishes a "local environment" which has the useful property that when the batch ends, all variables modified when running the batch are restored to their original values (those they had when the batch started). This prevents a subsequent batch that is run in the same session from inheriting the modifications made by this batch.
Next issue is the format of your set
statements. 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.
And a little quirk: SET /P "var=Prompt"
does not change var
if Enter alone is pressed. Consequently, if var
is originally empty, it remains empty. With the quotes as shown, you can put a space at the end of Prompt
.
I'd suggest it would be better if you displayed your list before you ask for a selection - it might just make it a little easier.
You have (potentially) a great long list to display, some entries in which are empty. You could modify your %%a
loop to …do if defined country[%%a] echo …
which may shorten the dispayed list by not showing any country[?]
that has not been defined.
I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables)
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs
(See `for/f` from the prompt for documentation)
Variables in batch are always strings. The mathematical functions available convert the variable contents to numerics to perform the calculations, then back again to strings to be assigned or echo
ed. Consequently, "converting" C
to C_num
actually achieves nothing. country[%C%]
is exactly the same as country[%C_num%]
.
You haven't told us what you get when you try ECHO %country[!C_num!]%
. You need a thorough understanding of Delayed expansion
So - you need to first invoke delayedexpansion
and then ECHO !country[%C_num%]!
On the other hand, you could try adapting this: Menu with 3 columns which uses choice
- preferred because it obviates processing of potential random user-input.