batch file - local variables not comparable?

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
16,288
I am working on a batch file - yes, I know it owul be preferable to use something else, but this is what I am stuck with.

I am using
SETLOCAL ENABLEEXTENSIONS
to make sure DEFINED will be able to be used.

SETLOCAL makes it so that any variables defined inside the batch file will only be seen inside the batch file.

Now, when I go to check if an environment variable exists, it works.

If DEFINED ProgramFiles(x86)

But if I make a local variable such as:

SET My_Var=1

It gives me a "syntax is incorrect" error anytime I try to do anything besides echo the variable.

I have tried

IF DEFINED My_Var (do something)

IF "%My_Var%"==1 (do something)

IF %My_Var% (do something)

and a few others.

If I do a @ECHO %My_Var% it returns 1.
 
Think I found a bug in the MS batch file parser. Of course MS would say that it is "working as designed".

It seems to work IF the whole command is on one line or IF I use a caret to continue the command onto the next line.

This only happens with a couple of the local variables so far. Some have to be continued with a caret to the next line and some don't.

Makes no sense whatsoever.
 
Can you write a minimal, complete and verifiable example. I was unable to reproduce your issue on Windows 7.

I could just post the whole 350+ line file.

I got it down to only happening with the very first local variable I try to compare.

If I change the value of the variable or change the variable name it messes it up and makes it not work, so not exactly sure what it going on with it.
 
Not sure what happened. I posted this earlier but it is not showing up now.

I found out that I was missing a space later on in the batch file and for some reason it was messing it up earlier on.

See commented code:

Code:
@ECHO OFF

SET OS_bitness=64
  
SET OFFICE_2010_32bit=1
SET OFFICE_32bit=1
SET OFFICE_DETECTED=1
   

REM --------------------------------------------------
REM Next line is where it messes up
REM If you uncomment the one with the caret and comment the one without the caret, it magically starts working
REM IF DEFINED OFFICE_32bit( <nul ^
IF DEFINED OFFICE_32bit ( 
REM --------------------------------------------------
REM @ECHO office 32bit
IF DEFINED OFFICE_DETECTED (
REM @ECHO office detected
IF DEFINED OFFICE_2010_32bit (
REM @ECHO Office 2010 32-bit installed
         @ECHO Office 2010 32bit
	  )  
   )	  
) ELSE (
   IF OS_bitness EQU 64 (
      IF DEFINED OFFICE_DETECTED (
REM --------------------------------------------------------
REM Next line is where the "error" is. There should be a space between 4 & (
REM Line below was originally line 214 and where it messed up was originally line 174
	     IF DEFINED OFFICE_2010_x64(
            @ECHO Office 2010 x64
		 )
      )
   )
)
 
Back
Top