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

Problem with variable names with embedded period

Discussion in 'Support' started by Steve Fabian, Nov 19, 2008.

  1. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    It seems that under many circumstances the parser considers a period
    embedded in the name of an environment variable as a name terminator. Some
    instances I found are:

    [c:]set setcolor.lst=aa bb cc
    [c:]set setcolor=xxx

    1/ using the VariableExpand key in the command line

    [c:]xyz %setcolor.lst

    displays

    [c:]xyz xxx.lst

    2/ ECHO

    [c:]echo %setcolor.lst
    xxx.lst

    3/ In a batch file the command

    do %x in /l %setcolor.lst
    echo %x
    enddo

    displays .lst



    IF DEFINED works correctly, i.e., if SETCOLOR is not defined, IF DEFINED
    SETCOLOR.LST is true.

    The behavior goes back at least to V6. I never observed it, because I only
    use a few such variable names, and those only with IF DEFINED tests (not
    counting executable extensions).
    --
    Steve
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,374
    Likes Received:
    40
    The preferred characters for variable names are alphanumerics, dollar sign, and underscore. Adding the period to the list would wreak havoc on a large number of batch files....
     
  3. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Charles Dye wrote:
    | ---Quote (Originally by Steve Fábián)---
    | It seems that under many circumstances the parser considers a period
    | embedded in the name of an environment variable as a name terminator.
    | Some instances I found are: ---End Quote---
    | The preferred characters for variable names are alphanumerics, dollar
    | sign, and underscore. Adding the period to the list would wreak
    | havoc on a large number of batch files....

    You are correct that the period is NOT listed as a "standard" character, and
    HELP topic "environment.htm" specifies that period and other characters can
    be used in variable names that are expected to be expanded by enclosing the
    names in brackets, which I failed to do. I failed to RTFM...

    However, though rarely used nowadays, PKZIP.EXE uses the variable PKZIP.CFG
    to find the directory where its configuration file is, even in the
    COMMAND.COM environment. Defining executable extensions also requires
    variable names starting with period. I never had trouble defining and using
    them, but none of these variables are normally expanded at the command line
    or in batch programs.
    --
    Steve
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    10,092
    Likes Received:
    85
    Steve Fábián wrote:

    It goes back to v1 -- a period is not and never has been a valid
    character in a variable name.

    If you're really determined (and don't mind breaking most of the batch
    files in existence), you can force it with the %[] syntax.

    Rex Conn
    JP Software
     
  5. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    rconn wrote:
    | Steve Fábián wrote:
    |
    |
    | ---Quote---
    || It seems that under many circumstances the parser considers a period
    || embedded in the name of an environment variable as a name terminator.
    | ---End Quote---
    | It goes back to v1 -- a period is not and never has been a valid
    | character in a variable name.
    |
    | If you're really determined (and don't mind breaking most of the batch
    | files in existence), you can force it with the %[] syntax.

    I already admitted that I did not RTFM before posting the original message,
    and that there are no problems in 4NT or TCC operation. However, as a
    theoretical issue, if you consider period to be invalid in variable names,
    why does the syntax defining executable extensions require the creation of
    variables with period as first character of their name?
    --
    Steve
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    10,092
    Likes Received:
    85
    Steve Fábián wrote:

    So that they couldn't possibly be confused with an environment variable.
    (Just like the current working directory variables, which begin with a
    '=' and are hidden from view.)

    Rex Conn
    JP Software
     
  7. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,129
    Likes Received:
    33
    On Wed, 19 Nov 2008 20:59:12 -0600, Steve Fábián <> wrote:


    I don't think it's "invalid"; it's just special (and thus may need the %[]
    treatment). That's for a very good reason ... so you can do the likes of
    "%basename.txt". You wouldn't want to be required to say "%[basename].txt",
    would you?

    As for variables whose first character is "." they're special (in a way); what
    precedes the "." is an empty string and an empty string can't be the name of an
    environment variable.

    These are trade-offs for ease of use and I think 4NT does a pretty good job
    here.
     

Share This Page