ProgramFiles Path Test on x64

Discussion in 'Windows 64bit' started by Jerald Pratt, Feb 13, 2008.

  1. Jerald Pratt

    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
    Jerald Pratt, Feb 13, 2008
    #1
    1. Advertising

  2. @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 in message
    news:...
    > 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
    Charlie Russel - MVP, Feb 13, 2008
    #2
    1. Advertising

  3. Jerald Pratt

    Jerald Pratt Guest

    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
    Jerald Pratt, Feb 13, 2008
    #3
  4. 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" <> wrote in message
    news:...
    > 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
    Charlie Russel - MVP, Feb 13, 2008
    #4
  5. 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" <> wrote in message
    news:...
    > 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
    Charlie Russel - MVP, Feb 13, 2008
    #5
  6. Jerald Pratt

    Jerald Pratt Guest

    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.
    >
    >
    Jerald Pratt, Feb 15, 2008
    #6
  7. 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" <> wrote in message
    news:...
    > 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.
    >>
    Charlie Russel - MVP, Feb 15, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    test test test test test test test

    Guest, Jul 2, 2003, in forum: Computer Support
    Replies:
    2
    Views:
    923
    halfalifer
    Jul 2, 2003
  2. DW
    Replies:
    7
    Views:
    26,046
    lbellysmit
    Jan 3, 2011
  3. =?Utf-8?B?cmluZGk=?=

    unattended and changing programfiles dir

    =?Utf-8?B?cmluZGk=?=, Feb 14, 2006, in forum: Windows 64bit
    Replies:
    0
    Views:
    594
    =?Utf-8?B?cmluZGk=?=
    Feb 14, 2006
  4. JH
    Replies:
    2
    Views:
    1,896
    Charlie Russel - MVP
    Apr 30, 2008
  5. santhosh.kulandaiyan

    %programfiles% environment variable : Native Mode and WOW Mode

    santhosh.kulandaiyan, Jul 17, 2008, in forum: Windows 64bit
    Replies:
    0
    Views:
    1,823
    santhosh.kulandaiyan
    Jul 17, 2008
Loading...

Share This Page