Search code examples
batch-filenested-if

batch file nested if statment crashes


I am trying to learn batch by writing simple programs, but I had a problem that is completely stopping me. I figured out that it kept crashing on the nested if statements, but as soon as I removed the nested part, it worked fine.

set questionNumber=1
if %questionNumber%==1 (
    set /p answer=Test?
    if %answer%==yes (
        echo hi
    )
)
pause>nul

Solution

  • setlocal enableDelayedExpansion
    set questionNumber=1
    if %questionNumber%==1 (
        set /p answer=Test?
        if !answer!==yes (
            echo hi
        )
    )
    pause>nul
    

    Delayed expansion