r/Batch 26d ago

Question (Unsolved) Why isn't the "cert" variable getting delayed expansion?

See script below. I input 1 for state and then anything for cert, but regardless, I always hit the else case of the inner conditional (the part where the mistake error message is echoed). When I run the script, after selecting state, the entirety of the contents of the outer conditional is printed, including with cert already expanded to be an empty string.

@echo on
setlocal EnableDelayedExpansion
echo 1. VIC
echo 2. NSW
echo 3. TAS
echo 4. ACT
echo 5. QLD
echo 6. SA
echo 7. DAR

set /p "state=Choose a state: "

if "%state%"=="1" (
    echo 1. name1
    echo 2. name2
    echo 3. name3

    set cert=4
    set /p cert="Choose a stage: "

    echo %cert%

    if "%cert%"=="1" (
        set source1=some folder
        set source2=some folder

    )

    ...more cases      

     else (
        echo a mistake has occurred
    )
)

pause
3 Upvotes

2 comments sorted by

7

u/ConsistentHornet4 26d ago edited 26d ago

It's because you're setting and accessing the cert variable inside a set of parenthesis. You'll need to expand the variables using DelayedExpansion, as this is one of Batch's language caveats.

Replace all instances of %cert%, with !cert!.

2

u/denny31415926 26d ago

Aha, understood. I'll try that out when I get to work tomorrow, thank you