Search code examples
windowsbatch-filegoto

Windows batch file: GOTO, DELAYEDEXPANSION issue


See my batch below!

Expected output: Success

Current output: Success

            Error

Any idea, why could be the problem? Without GOTO works as expected.

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION



SET doIt=1

SET callMeAgain=1

if !doIt! == 1 (

    :loop
    if !callMeAgain! == 1 (
        SET callMeAgain=0
        GOTO :loop
    )

    ECHO Success

) ELSE (

    ECHO Error
)


ECHO.

ENDLOCAL

PAUSE

Solution

  • A GOTO breaks any parenthesis block.

    A minimal example

    if 1==1 (
       echo success
       goto :dummy
       :dummy
    ) ELSE (
       echo ERROR
    )
    

    After the jump to :dummy the batch parser looks at:

    ) ELSE (
       echo ERROR
    )
    

    The IF part is ignored at this point.
    The ) ELSE ( part is simply ignored, because a single opening ) works like a line comment here.