0

I have a cmd file. It receives two input parameters. The first parameter is the product name and the second the version.

I have a string variable that I create and initialize to a string with value "false" and then depending if some conditions are satisfied I update this variable to "true".

Finally I have a conditional in which I check if the value of the string variable is "true". The problem is that the comparison in this conditional fails so it does not enter within the conditional.

I have put an echo just before this conditional and it outputs an empty string.

below the piece of code:

@ECHO OFF
    
IF /I "%~1"=="product_C" (
   REM DO sth
) ELSE (
    IF /I "%~1"=="product_D" (
       REM Do sth
    ) ELSE (
       SET "product_a_or_b=false"    
       IF /I "%~1"=="Product_A" (
           SET "product_a_or_b=true"
       )
    
       IF /I "%~1"=="Product_B" (
           REM I have checked that this conditional is satisfied and enters here with an ECHO.
           SET "product_a_or_b=true"
       )
    
       REM Below if-conditional is not satisfied despite I set the variable to "true" in the previous ifs
       IF "%product_a_or_b%"=="true" (
           ECHO "It is product A or B"
       )
    )
)
Willy
  • 9,848
  • 22
  • 141
  • 284
  • Your code snipped is working fine. You should include the outer conditional to make your issue reproducable. ([solution](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028)) – Stephan Jul 26 '23 at 07:22
  • @Stephan I have updated the post with the outer conditional. – Willy Jul 26 '23 at 07:36
  • 1
    Besides the delayed expansion solution - are you aware that you could rewrite your code without a single `ELSE` (which won't need delayed expansion)? (`if A ... else if B ...` is the same as `if A ...` and `if B ...`) – Stephan Jul 26 '23 at 07:47
  • @Stephanyep, finally i did it. thx Also thank you about the delayed expansion. I didn't know it. – Willy Jul 26 '23 at 11:34
  • yes, it's a strange concept. I know of no other language that does something similar. Well, once you know, you'll adapt. – Stephan Jul 26 '23 at 16:20

0 Answers0