35

Whats wrong with this code?

IF "%language%" == "de" (
    goto languageDE
) ELSE (
    IF "%language%" == "en" (
    goto languageEN
) ELSE (
    echo Not found.
)

I'm not really good in Batch..

1
  • 17
    Compare your parens to your indents. Should be illuminating.
    – cHao
    Aug 19, 2014 at 13:17

6 Answers 6

58
@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.

2
  • Can you add these shortcuts inside a for loop?
    – atwellpub
    Oct 19, 2016 at 18:28
  • 1
    The /I switch to IF does case-insensitive string comparisons. You can type HELP IF for more information about IF.
    – Leonel
    Apr 13, 2020 at 13:24
27
@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.

1
  • 2
    Don't forget that after echo Not found. it will drop through. So call exit or goto somewhere else. May 27, 2021 at 21:23
12

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

goto :language%language%
echo notfound
...

does this without any need for if.

1
  • Ok additional thing: What if there could be the case that the label is not defined? Any way to get around WITH this code?
    – C4d
    Sep 30, 2016 at 14:41
1

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
1

As @cHao commented, the opening parentheses number is higher (+1) than the closing ones'. The 2 need to be equal (to be more precise, the "(" just after the 1st ELSE should not be there).

Check [MS.Learn]: if (or help if, personally I prefer [SS64]: IF) for more details.

Because of the fact that each branch only contains a [SS64]: GOTO, the whole block could be rewritten as 2 independent If commands (simpler), but this is a generic form and other commands could be added in each branch (yes, Batch does allow (some degree of) structuring).

script00.bat:

@echo off


if "%LANGUAGE%" == "de" (
    goto :languageDE
) else if "%LANGUAGE%" == "en" (
    goto :languageEN
) else (
    echo Not found.
)


goto :eof


:languageDE
    echo Deutsche.
    goto :eof


:languageEn
    echo English.
    goto :eof

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q025384358]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> dir /b
script00.bat

[prompt]>
[prompt]> for %g in (dummy de ro en x) do (set LANGUAGE=%g& script00.bat)

[prompt]> (set LANGUAGE=dummy  & script00.bat)
Not found.

[prompt]> (set LANGUAGE=de  & script00.bat)
Deutsche.

[prompt]> (set LANGUAGE=ro  & script00.bat)
Not found.

[prompt]> (set LANGUAGE=en  & script00.bat)
English.

[prompt]> (set LANGUAGE=x  & script00.bat)
Not found.
0
@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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.