0

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
)
DarkMesa
  • 15
  • 1
  • 6
  • Batch files do not crash as batch files are plain text files with command lines interpreted by `cmd.exe` which is the Windows command interpreter. `cmd.exe` processes each command line respectively command block starting with `(` and ending with `)`, checks syntax and exits batch file processing if the syntax is incorrect with an appropriate error message which can be read by batch file writer/user to fix the incorrect code. See [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). – Mofi Jan 06 '18 at 13:09
  • What is the purpose of `)` after the line `echo %cd%\%location%\%customname%`? It looks like the line above this line should be `) else (` instead of just `)`. There is one more `)` at end with no matching `(` somewhere above. And please don't use `set /P` for simple yes/no prompt. There is the command __CHOICE__ for such purposes. Open a command prompt window and run `choice /?` for help on this command explaining how to use it and read Microsoft support article [Testing for a Specific Error Level in Batch Files](https://support.microsoft.com/en-us/kb/69576). – Mofi Jan 06 '18 at 13:14
  • Some more hints: As help of command __IF__ explains output on running `if /?` in a command prompt window, it is also possible to do a case-insensitive string comparison using additionally option `/I` after command __IF__ before first string to compare. However, use __CHOICE__ and the last 13 lines can be optimized to just 2 or 3 lines and user cannot make anymore a wrong input. Every standard Windows command can be executed with `/?` as option to get displayed the help for this command. Read also about [delayed expansion](https://ss64.com/nt/delayedexpansion.html). – Mofi Jan 06 '18 at 13:24

1 Answers1

1

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • I have fixed the issues by using single line "IF '%1%'=='%2%' echo TEXT // and "IF NOT...". I usually mess up the Parentheses I'm guessing, I'll try to indent the code so I can easily catch Mistakes next time. – DarkMesa Jan 06 '18 at 14:18
  • 1
    @DarkMesa, try not to use **`'`** over **`"`**, the latter will better preserve the contents within. I **Edit**ed my answer with your above snippet minus all parentheses. – Compo Jan 06 '18 at 15:07