batch-filecommandbatch-choice

What am i doing wrong using the Choice command with batch?


This is not the full code, since its about 1000 lines long, but here's the problem, when i come to this section of the game

choice /c abc1 /n

when i press "a" it's supposed to "medicalbag" and instead it acts like if i were to press "1" and goes back to start

when i press "b,c,1" they all go to "medicalbag".

i can't find a solution to this, i read about the command and apparently it supports these letters and numbers, when i change them out with just numbers they work just fine, but im really not sure what im doing wrong here.

:bag 
cls
echo                    *****************************
echo                    a) Medical supplies
echo                    b) Consumables
echo                    c) Weaponry
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c abc1 /n

if %errorlevel% == a goto medicalbag
if %errorlevel% == b goto consumablebag
if %errorlevel% == c goto weaponrybag
if %errorlevel% == 1 goto start

:medicalbag
cls
echo                    *****************************
echo                    Bandages: %bandagecount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c 1 /n

if %errorlevel% == 1 goto bag

:consumablebag
cls
echo                    *****************************
echo                    Canned food: %cannedfoodcount%
echo                    Purified water: %purifiedwatercount%
echo                    Dirty water: %dirtywatercount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c 1 /n

if %errorlevel% == 1 goto bag

:weaponrybag
cls
echo                    *****************************
echo                    a) combatknife: %combatknifecount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c a1 /n

if %errorlevel% == a goto combatknifecheck
if %errorlevel% == 1 goto bag

Solution

  • the errorlevel of the choice keys defined using the /c switch is returned according to the keys position in the list.

    with /c abc1:

    keypress:   errorlevels reuturned:
    a           1
    b           2 
    c           3
    1           4
    

    To get the literal keypress, you need to use a for /f loop to capture the keypress:

    For /f "delims=" %%G in ('choice /n /c abc1')Do Echo(You pressed: %%G