Batch question

Discussion in 'Programming' started by Kyllian, May 29, 2017.

  1. Hey. I'm experimenting with Batch to make a boot script.

    But I have issues with it. I got a script from internet.

    I have this now

    Code (Text):
    @echo off
    setlocal EnableExtensions

    title Server boot

    set "App[1]=1.8"
    set "App[2]=1.11"

    :: Display the Menu
    set "Message="
    :Menu
    cls
    echo.%Message%
    echo.
    echo.  Boot minecraft server
    echo.
    set "x=0"
    :MenuLoop
    set /a "x+=1"
    if defined App[%x%] (
        call echo   %x%. %%App[%x%]%%
        goto MenuLoop
    )
    echo.

    :: Prompt User for Choice
    :Prompt
    set "Input="
    set /p "Input=Select the version to boot from:"

    :: Validate Input [Remove Special Characters]
    if not defined Input goto Prompt
    set "Input=%Input:"=%"
    set "Input=%Input:^=%"
    set "Input=%Input:<=%"
    set "Input=%Input:>=%"
    set "Input=%Input:&=%"
    set "Input=%Input:|=%"
    set "Input=%Input:(=%"
    set "Input=%Input:)=%"
    :: Equals are not allowed in variable names
    set "Input=%Input:^==%"
    call :Validate %Input%
    goto End


    :Validate
    set "Next=%2"
    if not defined App[%1] (
        set "Message=Invalid Input: %1"
        goto Menu
    )
    if defined Next shift & goto Validate
        call echo %%App[%1]%%
        call if "%%App[%1]%%" == "1.8" (
        echo.Yep, it's 1.8
        )
    goto :eof

    :: Prevent the command from being processed twice if listed twice.
    set "App[%1]="
    if defined Next shift & goto Process
    goto :eof


    :End
    endlocal
    pause >nul
    But when I enter 2. (1.11.2) it still put Yep, it's 1.8 out

    So how do I compare strings then?
     
  2. Strahan

    Benefactor

    You are way over complicating it. Just use CHOICE instead of set /p, it handles validation for you as it only accepts what you tell it to accept. As for comparison, pretty sure you can't nest like that. At least, not without using delayed expansion.
     
  3. I now tried this:

    It seems to work quite good!

    Code (Text):
    @echo off
    setlocal EnableExtensions

    title Server boot

    :Menu
    cls
    echo.Boot minecraft server
    echo.
    echo.    Versions:
    echo.  
    echo.    1. 1.7
    echo.    2. 1.8
    echo.    3. 1.9
    echo.    4. 1.10
    echo.    5. 1.11
    echo.    6. 1.12
    echo.

    :: Prompt User for Choice
    :Prompt
    set "Input="
    set /p "Input=Please enter a version you'd like to launch: "

    :: Validate Input [Remove Special Characters]
    if not defined Input goto Prompt
    set "Input=%Input:"=%"
    set "Input=%Input:^=%"
    set "Input=%Input:<=%"
    set "Input=%Input:>=%"
    set "Input=%Input:&=%"
    set "Input=%Input:|=%"
    set "Input=%Input:(=%"
    set "Input=%Input:)=%"
    :: Equals are not allowed in variable names
    set "Input=%Input:^==%"
    call :Validate %Input%
    goto End


    :Validate
    if defined Next shift & goto Validate
        if %Input% == 1 (
        echo.Now booting 1.7...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        )
            if %Input% == 2 (
        echo.Now booting 1.8...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        )
            if %Input% == 3 (
        echo.Now booting 1.9...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        )
            if %Input% == 4 (
        echo.Now booting 1.10...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        )
            if %Input% == 5 (
        echo.Now booting 1.11...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        )
            if %Input% == 6 (
        echo.Now booting 1.12...
        @ECHO OFF
        PING 1.1.1.1 -n 1 -w 2000 >NUL
        exit
        ) else (
        echo.
        echo.Invalid input! Please try again
        echo.
        goto Prompt
        )