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

What's happening here?

Discussion in 'Support' started by vefatica, Jun 6, 2008.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,056
    Likes Received:
    30
    What's going on here?

    v:\> type ztest.btm
    set a=100
    set b=%a
    set /a c=%a
    set /a d=a
    echo %a %b %c %d

    v:\> for /l %a in (5,1,6) ztest.btm
    5 5 5 100
    6 6 6 100

    The second and third SET commands, and the ECHO command use the value of "a"
    from the FOR loop.

    The fourth SET uses the value of "a" set in the batfile.

    The same thing happens even after SETLOCAL (which I thought might hide FOR's
    counter from the batfile).

    v:\> type ztest.btm
    setlocal
    set a=100
    set b=%a
    set /a c=%a
    set /a d=a
    echo %a %b %c %d

    v:\> for /l %a in (5,1,6) ztest.btm
    5 5 5 100
    6 6 6 100
    --
    - Vince
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,994
    Likes Received:
    84
    vefatica wrote:

    FOR variables are not environment variables; the syntax in your fourth
    SET only works with environment variables.

    It's generally a really bad idea to use the same names for environment
    variables and FOR variables!

    Rex Conn
    JP Software

    p.s.: Your syntax fails miserably in CMD ...
     
  3. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,351
    Likes Received:
    39
    Strange, perverse, and not very useful, but documented; see "Other Notes" under help for the FOR command. It should go without saying that this craziness is for CMD.EXE compatibility. You can prevent this behavior simply by giving the FOR variable a two-character (or longer) name.
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,056
    Likes Received:
    30
    On Fri, 06 Jun 2008 13:21:34 -0500, you wrote:



    Then it appears completely backwards. If the for variable isn't an envvar, why
    do the other three SETs use it?


    Does that mean if I want to test **any** batfile with a command line FOR /L, I
    have to know in advance whether FOR's counter is used inside the batfile?
    --
    - Vince
     
  5. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,994
    Likes Received:
    84
    vefatica wrote:


    Because you used the % in front of the name.


    > batfile?

    If you want to use FOR for *anything*, inside or outside a batch file,
    you have to know in advance whether FOR's variable is used inside the
    batchfile. (This behavior goes back to 4DOS 1.0.) Unlike CMD, FOR in
    TCC allows you to use a multi-character variable name:

    for /l %wankelrotaryengine in (1,1,10) do ...

    Having FOR variables work in the FOR target command is a critical
    feature; changing this would break nearly every FOR loop in existence.

    Rex Conn
    JP Software
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,056
    Likes Received:
    30
    I'm getting very confused. Why do these behave differently?

    v:\> type ztest.btm
    setlocal
    unset /q a & REM would say not in environment
    set a=100
    set b=%a
    set /a c=%a
    set /a d=a
    echo %a %b %c %d

    v:\> for /l %a in (5,1,6) ztest.btm
    5 5 5 100
    6 6 6 100

    v:\> type ztest2.btm
    setlocal
    unset * & REM apparently UNSETs "a"
    set a=100
    set b=%a
    set /a c=%a
    set /a d=a
    echo %a %b %c %d

    v:\> for /l %a in (5,1,6) ztest2.btm
    100 100 100 100
    100 100 100 100
    --
    - Vince
     
  7. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,994
    Likes Received:
    84
    vefatica wrote:

    I don't understand why you persist in overloading your variable names.
    Stop doing that and all your problems go away!

    "UNSET *" removes all variables, including FOR variables.

    Rex Conn
    JP Software

    p.s.: Your recent use of TCC-only "SET /A" syntax is damaging your
    previous argument for CMD-compatible syntax ...
     
  8. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,056
    Likes Received:
    30
    On Sat, 07 Jun 2008 15:11:00 -0500, you wrote:


    When I go to test a batfile inside a command line "FOR /L" I don't want to have
    to worry about the name of FOR's control variable; I don't want to think about
    it at all. It could be a batfile I haven't looked at in ages, or even one
    written by someone else.
    --
    - Vince
     
  9. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,994
    Likes Received:
    84
    vefatica wrote:
    >

    Which is why you should be using something other than a 1-character
    variable name in your command line FOR!

    Rex Conn
    JP Software
     
  10. BillMc

    BillMc Guest

    On Sat 7-Jun-08 8:58pm -0600, rconn wrote:


    A better solution is not to use, except in for constructs,
    one letter variables. These don't conflict.

    For example, here's a one-line fortest.btm file:

    for /l i in (1,1,5) echos %i&echo.

    Running it with:

    for /l i in (1,1,5) (ftst&echo %i)

    Correctly displays:

    12345
    1
    12345
    2
    12345
    3
    12345
    4
    12345
    5

    Note also that I don't use a '%' in front of the for index
    variable. I've been doing this since version 7 or 8 without
    a problem.

    --
    Best regards,
    Bill
     

Share This Page