Batch - If, ElseIf, Else

前端 未结 6 704
谎友^
谎友^ 2021-01-07 18:55

Whats wrong with this code?

IF \"%language%\" == \"de\" (
    goto languageDE
) ELSE (
    IF \"%language%\" == \"en\" (
    goto languageEN
) ELSE (
    ech         


        
相关标签:
6条回答
  • 2021-01-07 19:16
    @echo off
    title Test
    
    echo Select a language. (de/en)
    set /p language=
    
    IF /i "%language%"=="de" goto languageDE
    IF /i "%language%"=="en" goto languageEN
    
    echo Not found.
    goto commonexit
    
    :languageDE
    echo German
    goto commonexit
    
    :languageEN
    echo English
    goto commonexit
    
    :commonexit
    pause
    

    The point is that batch simply continues through instructions, line by line until it reaches a goto, exit or end-of-file. It has no concept of sections to control flow.

    Hence, entering de would jump to :languagede then simply continue executing instructions until the file ends, showing de then en then not found.

    0 讨论(0)
  • 2021-01-07 19:21
    @echo off title Test
    echo Select a language. (de/en) 
    set /p language=
    
    IF "%language%" == "de" (
       goto :languageDE 
       ) ELSE IF "%language%" == "en" ( 
       goto :languageEN 
      ) ELSE (
       echo Not found. )
    
    :languageDE 
       echo welcome ,you selected the jarmni langauge 
       goto :eof
    
    :languageEN 
       echo welcome ,you selected the english langauge 
       goto :eof
    
    0 讨论(0)
  • 2021-01-07 19:24

    batchfiles perform simple string substitution with variables. so, a simple

    goto :language%language%
    echo notfound
    ...
    

    does this without any need for if.

    0 讨论(0)
  • 2021-01-07 19:33
    @echo off
    color 0a
    set /p language=
    if %language% == DE (
        goto LGDE
    ) else (
        if %language% == EN (
        goto LGEN
        ) else (
        echo N/A
    )
    
    :LGDE
    (code)
    :LGEN
    (code)
    
    0 讨论(0)
  • 2021-01-07 19:35

    Recommendation. Do not use user-added REM statements to block batch steps. Use conditional GOTO instead. That way you can predefine and test the steps and options. The users also get much simpler changes and better confidence.

    @Echo on
    rem Using flags to control command execution
    
    SET ExecuteSection1=0
    SET ExecuteSection2=1
    
    @echo off
    
    IF %ExecuteSection1%==0 GOTO EndSection1
    ECHO Section 1 Here
    
    :EndSection1
    
    IF %ExecuteSection2%==0 GOTO EndSection2
    ECHO Section 2 Here
    :EndSection2
    
    0 讨论(0)
  • 2021-01-07 19:43
    @echo off
    
    set "language=de"
    
    IF "%language%" == "de" (
        goto languageDE
    ) ELSE (
        IF "%language%" == "en" (
            goto languageEN
        ) ELSE (
        echo Not found.
        )
    )
    
    :languageEN
    :languageDE
    
    echo %language%
    

    This works , but not sure how your language variable is defined.Does it have spaces in its definition.

    0 讨论(0)
提交回复
热议问题