-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,262
  • 2
  • 12
  • 22
  • 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

2 Answers2

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,262
  • 2
  • 12
  • 22
0

Why not loop For /L?

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