1

I would like to use a variable to set choices for the choice command but every time i use this code in a batch file, this is what it outputs ERROR: Invalid choice. The valid choice characters are: a-z, A-Z, 0-9 and ASCII values of 128 to 254.

set 1=1
set 2=2
set 3=3
choice /c %1%%2%%3% /n
if %errorlevel%==1 goto 1
if %errorlevel%==2 goto 2
if %errorlevel%==3 goto 3

What am i doing wrong here? Help would be appreciated, Thanks

maximuslotr
  • 74
  • 1
  • 1
  • 9
  • If you run your script from the command prompt, you'll clearly see what's happening. Then you'll learn not to use numbers as variable names. `choice /c %2%3 /n` I'll assume. – Compo Oct 13 '19 at 21:29
  • I suppose I should clarify that, you'll learn not to use them as the first character of a variable name. – Compo Oct 13 '19 at 21:48

1 Answers1

2

Do not use numeric variable names. You also do not need to do if errorlevel as you can simply goto in this instance, again I just added var before %errorlevel% to simply not use numerical characters only..

set "var1=1"
set "var2=2"
set "var3=3"
choice /c %var1%%var2%%var3% /n
goto var%errorlevel%

:var1
echo you chose var%errorlevel%
Gerhard
  • 22,678
  • 7
  • 27
  • 43