DarkMesa
DarkMesa

Reputation: 15

If (else) crashed batch file

My code is the Following, and it crashes when it reaches the first "if" statement, I am a novice and can't find a fix for this. There is nothing variable, and the if statements past that work fine. What is causing the Crash exactly? I've experienced this before, "if" crashing everything unless there is a "goto" line under it. Is the "echo" that's causing the Crash Perhaps?

:readytopack
cls
echo ========= ASSETS PACKER =========
echo Confirm the following Settings?
echo.
echo The folder to be packed is:
echo %filelocation%
echo %cd%\%filelocation%
echo Will be built to .pak in:
echo %location%
pause>nul
if "%customname%"=="0" (
echo %cd%\%location%
)
echo %cd%\%location%\%customname%
)
echo.
echo [yes] [no] (Lowercase)
echo Selecting "no" will revert a Step.
echo =================================
echo.
Set /p CMD=
if "%CMD%"=="" (
goto start
)
if "%CMD%"=="yes" (
goto pack
)
if "%CMD%"=="no" (
goto unpackerlocation
)
echo Please enter "yes" or "no" in Lowercase.
goto readytounpack
)

Upvotes: 0

Views: 1421

Answers (1)

Compo
Compo

Reputation: 38719

You appear to have either unbalanced parentheses, or have missed out part of the script and created labels within code blocks.

If you indent your code when you write it, you would find it easier to spot unbalanced parentheses.

Here it is indented from the bottom upwards:

        :readytopack
        cls
        echo ========= ASSETS PACKER =========
        echo Confirm the following Settings?
        echo.
        echo The folder to be packed is:
        echo %filelocation%
        echo %cd%\%filelocation%
        echo Will be built to .pak in:
        echo %location%
        pause>nul
        if "%customname%"=="0" (
            echo %cd%\%location%
        )
        echo %cd%\%location%\%customname%
    )
    echo.
    echo [yes] [no] (Lowercase)
    echo Selecting "no" will revert a Step.
    echo =================================
    echo.
    Set /p CMD=
    if "%CMD%"=="" (
        goto start
    )
    if "%CMD%"=="yes" (
        goto pack
    )
    if "%CMD%"=="no" (
        goto unpackerlocation
    )
    echo Please enter "yes" or "no" in Lowercase.
    goto readytounpack
)

Do lines 16 and 34 look correct to you? What happens if you remove those two lines?

Edit
In the above case, you could actually just remove all of those parentheses:

:readytopack
ClS
Echo ========= ASSETS PACKER =========
Echo Confirm the following Settings?
Echo=
Echo The folder to be packed is:
Echo=%filelocation%
Echo %cd%\%filelocation%
Echo Will be built to .pak in:
Echo=%location%
Pause>Nul
If "%customname%"=="0" Echo %cd%\%location%
Echo %cd%\%location%\%customname%
Echo=
Echo [yes] [no]
Echo Selecting "no" will revert a Step.
Echo =================================
Echo=
Set /P "CMD= "
If "%CMD%"=="" GoTo start
If /I "%CMD%"=="yes" GoTo pack
If /I "%CMD%"=="no" GoTo unpackerlocation
Echo Please enter "yes" or "no".
GoTo readytounpack

I would also suggest that you take a look at the Choice command, it is more robust than Set /P when you are expecting known strings or characters as input.

Upvotes: 1

Related Questions