What's happening here?

#1
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
 

rconn

Administrator
Staff member
May 14, 2008
10,567
97
#2
vefatica wrote:

> 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.
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 ...
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,609
46
Albuquerque, NM
prospero.unm.edu
#3
What's going on here?

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.
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
On Fri, 06 Jun 2008 13:21:34 -0500, you wrote:


>> 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.

>FOR variables are not environment variables; the syntax in your fourth
>SET only works with environment variables.
Then it appears completely backwards. If the for variable isn't an envvar, why
do the other three SETs use it?


>It's generally a really bad idea to use the same names for environment
>variables and FOR variables!
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
 

rconn

Administrator
Staff member
May 14, 2008
10,567
97
#5
vefatica wrote:


> Quote:
> >FOR variables are not environment variables; the syntax in your fourth
> >SET only works with environment variables.
>
> Then it appears completely backwards. If the for variable isn't an
> envvar, why do the other three SETs use it?
Because you used the % in front of the name.


> Quote:
> >It's generally a really bad idea to use the same names for environment
> >variables and FOR variables!
>
> 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?

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
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
 

rconn

Administrator
Staff member
May 14, 2008
10,567
97
#7
vefatica wrote:

> 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
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
On Sat, 07 Jun 2008 15:11:00 -0500, you wrote:


>I don't understand why you persist in overloading your variable names.
>Stop doing that and all your problems go away!
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
 

rconn

Administrator
Staff member
May 14, 2008
10,567
97
#9
vefatica wrote:
>

> Quote:
> >I don't understand why you persist in overloading your variable names.
> >Stop doing that and all your problems go away!
>
> 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.
Which is why you should be using something other than a 1-character
variable name in your command line FOR!

Rex Conn
JP Software
 
B

BillMc

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


> Which is why you should be using something other than a 1-character
> variable name in your command line FOR!
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