ProgramFiles Path Test on x64

  • Thread starter Thread starter Jerald Pratt
  • Start date Start date
J

Jerald Pratt

Guest
I've got a problem with a bat script where I'm test to see if an env
variable has been defined. On 32-bit OS this works fine but now I'm
seeing problem on 64-bit Windows (Vista). Here is the example line:

if DEFINED JAVA_HOME (
echo JAVA_HOME: %JAVA_HOME
)

On a 32-bit system, my JAVA_HOME is set to something like: C:\Program
Files\Java\jre1.5.0_06. But on a 64-bit system with the 32-bit java
installed, the same JAVA_HOME is set to: C:\Program Files
(x86)\Java\jre1.5.0_06.

The problem that I'm having is it seems that the extra "(x86)" define in
the variable is causing problem with the parenthesis in the "if"
statement. I would think that this should not matter since the "if
DEFINED" should only be checking to see if the variable is defined
(especially since I'm not using % to wrap the variable). When I run
this in a bat script I get a message complaining something about
"\Java\jre1.5.0_06" being an invalid statement (or something similar).

Has anyone else seen this? Its pretty easy to verify. Just create a
short bat script, define the variable and put the test statement that I
listed above.

Anyone know why this happens? Any ideas of how to work around this?

Jerald
 
Re: ProgramFiles Path Test on x64

@echo off
@REM program name: testit.bat
SET JAVA_HOME=%PROGRAMFILES(x86)%\Java

if defined %JAVA_HOME% (
echo java_home is: %Java_home%
)

C:\ > testit
java_home is: C:\Program Files (x86)\Java


Looks like it works to me. Notice I changed your if defined. You can't test
for a variable without saying it's a variable. If you don't use %VAR%,
you'll end up with the error you saw.


--
Charlie.
http://msmvps.com/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel


"Jerald Pratt" <jerpratt@cox.net> wrote in message
news:uzD6iagbIHA.6024@TK2MSFTNGP06.phx.gbl...
> I've got a problem with a bat script where I'm test to see if an env
> variable has been defined. On 32-bit OS this works fine but now I'm
> seeing problem on 64-bit Windows (Vista). Here is the example line:
>
> if DEFINED JAVA_HOME (
> echo JAVA_HOME: %JAVA_HOME
> )
>
> On a 32-bit system, my JAVA_HOME is set to something like: C:\Program
> Files\Java\jre1.5.0_06. But on a 64-bit system with the 32-bit java
> installed, the same JAVA_HOME is set to: C:\Program Files
> (x86)\Java\jre1.5.0_06.
>
> The problem that I'm having is it seems that the extra "(x86)" define in
> the variable is causing problem with the parenthesis in the "if"
> statement. I would think that this should not matter since the "if
> DEFINED" should only be checking to see if the variable is defined
> (especially since I'm not using % to wrap the variable). When I run this
> in a bat script I get a message complaining something about
> "\Java\jre1.5.0_06" being an invalid statement (or something similar).
>
> Has anyone else seen this? Its pretty easy to verify. Just create a
> short bat script, define the variable and put the test statement that I
> listed above.
>
> Anyone know why this happens? Any ideas of how to work around this?
>
> Jerald
 
Re: ProgramFiles Path Test on x64

Thanks for the info and what you suggest seems to work better but still
not quite right.

First off, all documentation that I've read says that you don't use the
% to wrap the variable when using DEFINED. If you google this (dos bat
"if defined") you should see many examples that do this.

Second, with what you suggest the base statement seems to work but I
have an else that isn't not working right. With the same statement I
end up executing both the true and false cases of the if-else clause:

set JAVA_HOME=C:\Program Files (x86)\Java\jre1.5.0_06

if DEFINED %JAVA_HOME% (
echo JAVA_HOME: %JAVA_HOME%
) else (
echo JAVA_HOME not defined!
)

If I remove the % around JAVA_HOME in the defined statement then the
error message I get is:
\Java\jre1.5.0_06 was unexpected at this time.

If I go back and redefine JAVA_HOME to be the standard 32-bit path then
this all works fine.

Jerald


Charlie Russel wrote:
> @echo off
> @REM program name: testit.bat
> SET JAVA_HOME=%PROGRAMFILES(x86)%\Java
>
> if defined %JAVA_HOME% (
> echo java_home is: %Java_home%
> )
>
> C:\ > testit
> java_home is: C:\Program Files (x86)\Java
>
>
> Looks like it works to me. Notice I changed your if defined. You

can't test for a variable without saying it's a variable. If you don't
use %VAR%, you'll end up with the error you saw.
>
> Charlie.
> http://msmvps.com/xperts64
> http://mvp.support.microsoft.com/profile/charlie.russel


Jerald Pratt wrote:
> I've got a problem with a bat script where I'm test to see if an env
> variable has been defined. On 32-bit OS this works fine but now I'm
> seeing problem on 64-bit Windows (Vista). Here is the example line:
>
> if DEFINED JAVA_HOME (
> echo JAVA_HOME: %JAVA_HOME
> )
>
> On a 32-bit system, my JAVA_HOME is set to something like: C:\Program
> Files\Java\jre1.5.0_06. But on a 64-bit system with the 32-bit java
> installed, the same JAVA_HOME is set to: C:\Program Files
> (x86)\Java\jre1.5.0_06.
>
> The problem that I'm having is it seems that the extra "(x86)" define in
> the variable is causing problem with the parenthesis in the "if"
> statement. I would think that this should not matter since the "if
> DEFINED" should only be checking to see if the variable is defined
> (especially since I'm not using % to wrap the variable). When I run
> this in a bat script I get a message complaining something about
> "\Java\jre1.5.0_06" being an invalid statement (or something similar).
>
> Has anyone else seen this? Its pretty easy to verify. Just create a
> short bat script, define the variable and put the test statement that I
> listed above.
>
> Anyone know why this happens? Any ideas of how to work around this?
>
> Jerald
 
Re: ProgramFiles Path Test on x64

Well, my first thought would be - if you insist on using CMD as a scripting
language...

Seriously, have you thought of using PowerShell here? Trying to get CMD to
handle this many special characters is really pushing it, since there's not
good or consistent way to escape them.

--
Charlie.
http://msmvps.com/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel


"Jerald Pratt" <jerpratt@cox.net> wrote in message
news:O86p5tgbIHA.3696@TK2MSFTNGP03.phx.gbl...
> Thanks for the info and what you suggest seems to work better but still
> not quite right.
>
> First off, all documentation that I've read says that you don't use the %
> to wrap the variable when using DEFINED. If you google this (dos bat "if
> defined") you should see many examples that do this.
>
> Second, with what you suggest the base statement seems to work but I have
> an else that isn't not working right. With the same statement I end up
> executing both the true and false cases of the if-else clause:
>
> set JAVA_HOME=C:\Program Files (x86)\Java\jre1.5.0_06
>
> if DEFINED %JAVA_HOME% (
> echo JAVA_HOME: %JAVA_HOME%
> ) else (
> echo JAVA_HOME not defined!
> )
>
> If I remove the % around JAVA_HOME in the defined statement then the error
> message I get is:
> \Java\jre1.5.0_06 was unexpected at this time.
>
> If I go back and redefine JAVA_HOME to be the standard 32-bit path then
> this all works fine.
>
> Jerald
>
>
> Charlie Russel wrote:
> > @echo off
> > @REM program name: testit.bat
> > SET JAVA_HOME=%PROGRAMFILES(x86)%\Java
> >
> > if defined %JAVA_HOME% (
> > echo java_home is: %Java_home%
> > )
> >
> > C:\ > testit
> > java_home is: C:\Program Files (x86)\Java
> >
> >
> > Looks like it works to me. Notice I changed your if defined. You

> can't test for a variable without saying it's a variable. If you don't use
> %VAR%, you'll end up with the error you saw.
> >
> > Charlie.
> > http://msmvps.com/xperts64
> > http://mvp.support.microsoft.com/profile/charlie.russel

>
> Jerald Pratt wrote:
>> I've got a problem with a bat script where I'm test to see if an env
>> variable has been defined. On 32-bit OS this works fine but now I'm
>> seeing problem on 64-bit Windows (Vista). Here is the example line:
>>
>> if DEFINED JAVA_HOME (
>> echo JAVA_HOME: %JAVA_HOME
>> )
>>
>> On a 32-bit system, my JAVA_HOME is set to something like: C:\Program
>> Files\Java\jre1.5.0_06. But on a 64-bit system with the 32-bit java
>> installed, the same JAVA_HOME is set to: C:\Program Files
>> (x86)\Java\jre1.5.0_06.
>>
>> The problem that I'm having is it seems that the extra "(x86)" define in
>> the variable is causing problem with the parenthesis in the "if"
>> statement. I would think that this should not matter since the "if
>> DEFINED" should only be checking to see if the variable is defined
>> (especially since I'm not using % to wrap the variable). When I run this
>> in a bat script I get a message complaining something about
>> "\Java\jre1.5.0_06" being an invalid statement (or something similar).
>>
>> Has anyone else seen this? Its pretty easy to verify. Just create a
>> short bat script, define the variable and put the test statement that I
>> listed above.
>>
>> Anyone know why this happens? Any ideas of how to work around this?
>>
>> Jerald
 
Re: ProgramFiles Path Test on x64

One way around it.

SET JAVA_HOME="%PROGRAMFILES(x86)%\Java" works as you expect. It works
because it delays the expansion of the path just long enough. So, the
following works as you want...

@echo off
SET JAVA_HOME="%PROGRAMFILES(x86)%\Java"

REM if /i %JAVA_HOME% EQU "C:\Program Files (x86)\Java" (
if defined Java_home (
echo java_home is: %Java_home%
) else (
echo Java_hone is NOT defined
)

(Note the alternative way to get at this that's REM'd out. )
--
Charlie.
http://msmvps.com/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel


"Jerald Pratt" <jerpratt@cox.net> wrote in message
news:O86p5tgbIHA.3696@TK2MSFTNGP03.phx.gbl...
> Thanks for the info and what you suggest seems to work better but still
> not quite right.
>
> First off, all documentation that I've read says that you don't use the %
> to wrap the variable when using DEFINED. If you google this (dos bat "if
> defined") you should see many examples that do this.
>
> Second, with what you suggest the base statement seems to work but I have
> an else that isn't not working right. With the same statement I end up
> executing both the true and false cases of the if-else clause:
>
> set JAVA_HOME=C:\Program Files (x86)\Java\jre1.5.0_06
>
> if DEFINED %JAVA_HOME% (
> echo JAVA_HOME: %JAVA_HOME%
> ) else (
> echo JAVA_HOME not defined!
> )
>
> If I remove the % around JAVA_HOME in the defined statement then the error
> message I get is:
> \Java\jre1.5.0_06 was unexpected at this time.
>
> If I go back and redefine JAVA_HOME to be the standard 32-bit path then
> this all works fine.
>
> Jerald
>
>
> Charlie Russel wrote:
> > @echo off
> > @REM program name: testit.bat
> > SET JAVA_HOME=%PROGRAMFILES(x86)%\Java
> >
> > if defined %JAVA_HOME% (
> > echo java_home is: %Java_home%
> > )
> >
> > C:\ > testit
> > java_home is: C:\Program Files (x86)\Java
> >
> >
> > Looks like it works to me. Notice I changed your if defined. You

> can't test for a variable without saying it's a variable. If you don't use
> %VAR%, you'll end up with the error you saw.
> >
> > Charlie.
> > http://msmvps.com/xperts64
> > http://mvp.support.microsoft.com/profile/charlie.russel

>
> Jerald Pratt wrote:
>> I've got a problem with a bat script where I'm test to see if an env
>> variable has been defined. On 32-bit OS this works fine but now I'm
>> seeing problem on 64-bit Windows (Vista). Here is the example line:
>>
>> if DEFINED JAVA_HOME (
>> echo JAVA_HOME: %JAVA_HOME
>> )
>>
>> On a 32-bit system, my JAVA_HOME is set to something like: C:\Program
>> Files\Java\jre1.5.0_06. But on a 64-bit system with the 32-bit java
>> installed, the same JAVA_HOME is set to: C:\Program Files
>> (x86)\Java\jre1.5.0_06.
>>
>> The problem that I'm having is it seems that the extra "(x86)" define in
>> the variable is causing problem with the parenthesis in the "if"
>> statement. I would think that this should not matter since the "if
>> DEFINED" should only be checking to see if the variable is defined
>> (especially since I'm not using % to wrap the variable). When I run this
>> in a bat script I get a message complaining something about
>> "\Java\jre1.5.0_06" being an invalid statement (or something similar).
>>
>> Has anyone else seen this? Its pretty easy to verify. Just create a
>> short bat script, define the variable and put the test statement that I
>> listed above.
>>
>> Anyone know why this happens? Any ideas of how to work around this?
>>
>> Jerald
 
Re: ProgramFiles Path Test on x64

Thanks for your assistance.

I believe I figured out what the problem is. Its not with the "if
defined JAVA_HOME" statement but with the use/reference to %JAVA_HOME%
within the () of the if-else statement; ie "echo JAVA_HOME is:
%JAVA_HOME%". If I put quotes around the use of %JAVA_HOME% then I
don't have a problem but when I don't quote it it seems that the close
parenthesis w/in the value of JAVA_HOME is seen as the close for the if
statement.

Jerald

Charlie Russel - MVP wrote:
> @echo off
> @REM program name: testit.bat
> SET JAVA_HOME=%PROGRAMFILES(x86)%\Java
>
> if defined %JAVA_HOME% (
> echo java_home is: %Java_home%
> )
>
> C:\ > testit
> java_home is: C:\Program Files (x86)\Java
>
>
> Looks like it works to me. Notice I changed your if defined. You can't test
> for a variable without saying it's a variable. If you don't use %VAR%,
> you'll end up with the error you saw.
>
>
 
Re: ProgramFiles Path Test on x64

Yup, see my response further down. You have to quote around the ()

--
Charlie.
http://msmvps.com/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel


"Jerald Pratt" <jerpratt@cox.net> wrote in message
news:47B527CB.3070301@cox.net...
> Thanks for your assistance.
>
> I believe I figured out what the problem is. Its not with the "if defined
> JAVA_HOME" statement but with the use/reference to %JAVA_HOME% within the
> () of the if-else statement; ie "echo JAVA_HOME is: %JAVA_HOME%". If I
> put quotes around the use of %JAVA_HOME% then I don't have a problem but
> when I don't quote it it seems that the close parenthesis w/in the value
> of JAVA_HOME is seen as the close for the if statement.
>
> Jerald
>
> Charlie Russel - MVP wrote:
>> @echo off
>> @REM program name: testit.bat
>> SET JAVA_HOME=%PROGRAMFILES(x86)%\Java
>>
>> if defined %JAVA_HOME% (
>> echo java_home is: %Java_home%
>> )
>>
>> C:\ > testit
>> java_home is: C:\Program Files (x86)\Java
>>
>>
>> Looks like it works to me. Notice I changed your if defined. You can't
>> test
>> for a variable without saying it's a variable. If you don't use %VAR%,
>> you'll end up with the error you saw.
>>
 
Back
Top