-1

I have the following value for the choice parameter:

Name: Param
Choices:

  • Test1
  • Test2
  • Test3

And an Execute windows batch command :

if (%Param% == "Test1") (
echo "1"
) else if (%Param% == "Test2") (
echo "2"
) else (
echo "3"
) ---is not working

if (%Param% == "Test1") (
echo "1"
) else ( if (%Param% == "Test2") (
echo "2") else (
echo "3"
)
) ---is not working
michael_heath
  • 5,157
  • 2
  • 10
  • 21
  • How is this related to jenkins? – choroba Mar 26 '19 at 16:55
  • Batch doesn't really support else like this. See this post. https://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file – Señor CMasMas Mar 26 '19 at 17:15
  • 2
    You need to change your comparisons, because the doublequotes are included. Change it to either this format `If "%Param%"=="Test1"`, _(preferred)_, or `If %Param%==Test1`. You should probably choose to use `If`'s `/I` option too, to make the comparison case insensitive. – Compo Mar 26 '19 at 17:31
  • As your example looks quite construed, your use case isn't clear to me. Do you want to validate a user input via `set /P` or passed as cmd line argument? –  Mar 26 '19 at 18:11

3 Answers3

1
set "Param=Test2"

if "%Param%" == "Test1" (
    echo "1"
) else if "%Param%" == "Test2" (
    echo "2"
) else (
    echo "3"
)

Almost had it in the 1st example. You do not enclose the comparison to test between ( and ) in batch-file.

Comparisons are literal, so what is on one side needs to match the other side. This includes the double quotes. So, variables without quotes may require them to match e.g. "%Param%" == "Test1". %Param% == "Test1" would never match in the example above as the value of %Param% has no double quotes.

michael_heath
  • 5,157
  • 2
  • 10
  • 21
0

Why not loop For /L?

For /l %%i In (1 1 3) Do If "%Param%"=="Test%%~i" Echo="%%i"
Io-oI
  • 2,386
  • 3
  • 17
  • 25
-2

Your fixed batch file should look like the following:

if "%Param%" == "Test1" (
    echo "1"
) else (
    if "%Param%" == "Test2" (
        echo "2"
    ) else (
        echo "3"
    )
)

if "%Param%" == "Test1" (
    echo "1"
) else (
    if "%Param%" == "Test2" (
        echo "2"
    ) else (
        echo "3"
    )
)

so... these two if statements, if expanded are exactly the same, unsure what do you want to do.

Note that:

  • Parentheses (()) and double quotes ("") were included in the comparison, so I removed them and double-quoted both strings.
  • I also expanded your if statements in order to make them clearer and you saw - they were the same.
double-beep
  • 4,567
  • 13
  • 30
  • 40