1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Environment variable oddity

Discussion in 'Support' started by Rod Savard, Oct 22, 2010.

  1. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    I am attempting to use the ProgramFiles(x86) variable in a batch file, but Take Command removes the space. See below -

    Code:
    C:\> ver
    
    TCC  12.00.29 x64   Windows 7 [Version 6.1.7600]
    
    C:\> set Prog
    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    ProgramW6432=C:\Program Files
    
    C:\> echo %ProgramFiles(x86)%
    C:\Program Files(x86)
    
    C:\>
     
  2. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    CMD behaves correctly:

    Code:
    C:\> set Prog
    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    ProgramW6432=C:\Program Files
    
    C:\> echo %ProgramFiles(x86)%
    C:\Program Files (x86)
    
    C:\>
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,190
    Likes Received:
    11
    Both of your examples show exactly the same thing.

    What's not working?

    As an alternative to what you are attempting, you may consider using short
    file names instead and avoid the whole space issue.

    -Scott

    Rod Savard <> wrote on 10/22/2010 06:10:47 PM:


     
  4. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    No, they don't show the same thing. Look more closely at the echo output in Take Command verses CMD.

    Take Command strips out a space, which makes it not work.

    How so? The whole reason I want to use this environment variable is because we don't know the Program Files folder is necessarily on the C: drive. Plus I want it to work regardless of CMD or Take Command being used (so I don't want to use @SHFOLDER).

    It SHOULD work -- there is something wrong with how Take Command is parsing this variable name. Perhaps the parens are confusing it.
     
  5. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,190
    Likes Received:
    11
    I see the issue now. The TCC parser stops parsing when it sees the open
    paren.

    If you change the ProgramFiles(x86) variable to C:\Program Files (x586)
    and re-run your test, you'll see the echo is the same:

    C:\> echo %ProgramFiles(x86)%
    C:\Program Files(x86)

    TCC is evaluating %ProgramFiles and then appending the (x86) text to that
    result.

    You can either not use parens, or use the bracketed syntax:
    echo %[ProgramFiles(x86)]

    -Scott

    Rod Savard <> wrote on 10/22/2010 06:08:24 PM:


     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    On Fri, 22 Oct 2010 18:08:26 -0400, Rod Savard <> wrote:

    |Code:
    |---------
    |C:\> ver
    |
    |TCC 12.00.29 x64 Windows 7 [Version 6.1.7600]
    |
    |C:\> set Prog
    |ProgramData=C:\ProgramData
    |ProgramFiles=C:\Program Files
    |ProgramFiles(x86)=C:\Program Files (x86)
    |ProgramW6432=C:\Program Files
    |
    |C:\> echo %ProgramFiles(x86)%
    |C:\Program Files(x86)
    |
    |C:\>
    |---------

    What you're getting there in the value of %ProgramFiles followed by the literal
    string "(x86)" (the trailing '%' contributes nothing).

    Get around it like this

    Code:
    v:\> set ProgramFiles(x86)=C:\Program Files (x86)
    
    v:\> set prog*
    ProgramFiles=C:\Program Files
    ProgramFiles(x86)=C:\Program Files (x86)
    
    v:\> echo %[Programfiles(x86)]
    C:\Program Files (x86)
    
     
  7. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,859
    Likes Received:
    83
    Perhaps a better phrase would be "CMD behaves as expected, if you don't expect much."

    CMD has no idea what a valid variable name is; it simply includes everything between two %'s. If you want TCC to behave the same, set "
    CMDVariables=YES" in your TCMD.INI. (Of course, then you'll have to rewrite all of your batch files and aliases.)
     
  8. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    I need the parens, because they are in the default environment table on Windows x64.

    And this workaround is nice for TC, but it is not CMD-compatible. I might as well use %@shfolder[42].
     
  9. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    Thanks, I may do this. I don't believe I do anything special with environment variables that would need to utilize the better TC parsing....but who knows.
     
  10. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    I wonder how many batch files would be broken by adding parentheses to the list of characters valid in variable names?
     
  11. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,859
    Likes Received:
    83
    Probably only a few zillion, provided all of the others don't use DO, FOR, or command groups.
     
  12. Rod Savard

    Joined:
    May 26, 2008
    Messages:
    481
    Likes Received:
    3
    Are parentheses butted up against variable names in do/for/command groups? Isn't there whitespace?
     
  13. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,859
    Likes Received:
    83
    Usually, though not always, for left parens. Almost never for right parens.
     

Share This Page