John3245
John3245

Reputation: 307

IF in batch file

If I do (MyVar is set to "hi" value)

IF %MyVar% == "hi" ( Echo Hi )

Then I got Output

Hi

But If I do ( ProgramFile(x86) is set to C:\Program Files (x86))

IF %ProgramFiles(x86)% == "C:\Program Files (x86)" ( Echo Hi )

then I got Out put

Files was unexpected at this time.

If I do:

 IF "%ProgramFiles(x86)%" == "C:\Program Files (x86)" ( Echo Hi )

Then I got NO output

Why output is not coming properly in case of ProgramFile(x86) environment variable?

Upvotes: 1

Views: 231

Answers (1)

shA.t
shA.t

Reputation: 16958

If Command Extensions are enabled IF changes as follows:

  • IF [/I] string1 compare-op string2 command
  • IF CMDEXTVERSION number command
  • IF DEFINED variable command

where compare-op may be one of:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed.

using compare-op with /I can be better;

so you can try this code:

IF /I "%ProgramFiles(x86)%" equ "C:\Program Files (x86)" (echo Hi)

But maybe the commend text is invalid:

The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is comparing against an internal version number associated with the Command Extensions. The first version is 1. It will be incremented by one when significant enhancements are added to the Command Extensions. CMDEXTVERSION conditional is never true when Command Extensions are disabled.

Upvotes: 1

Related Questions