How can I set a value to %%variable in the for /L loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I set a value to %%variable in the for /L loop?

#16 Post by aGerman » 18 Sep 2014 12:14

foxidrive wrote:
aGerman wrote:As mentioned before I don't see an easier way ...

I don't see any drawback from bars143's method, do you?

Well we are talking about nested loops. How do we return to the outer loop?

Even if you would find a way - how many times can you nest calling a lable without drawback?

Code: Select all

@echo off &setlocal
set /a "n = 0"

:foo
set /a "n += 1"
echo %n%
call :bar

:bar
goto foo

In my tests it counts until 705 then dies with the message that the maximum of recursions was reached.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How can I set a value to %%variable in the for /L loop?

#17 Post by foxidrive » 18 Sep 2014 12:42

aGerman wrote:
foxidrive wrote:
aGerman wrote:As mentioned before I don't see an easier way ...

I don't see any drawback from bars143's method, do you?

Well we are talking about nested loops. How do we return to the outer loop?

Even if you would find a way - how many times can you nest calling a lable without drawback?

Code: Select all

@echo off &setlocal
set /a "n = 0"

:foo
set /a "n += 1"
echo %n%
call :bar

:bar
goto foo

In my tests it counts until 705 then dies with the message that the maximum of recursions was reached.

Regards
aGerman


It wasn't for a nested loop (the discussion and participants moved on) - the last sample I commented on only uses one call.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I set a value to %%variable in the for /L loop?

#18 Post by aGerman » 18 Sep 2014 13:32

foxidrive wrote:the last sample I commented on only uses one call

I see. In that case I'd say we shouldn't expect any problems. (even if we normally should return to the point where the label was called for my understanding)

bars143 wrote:i found out why "goto" does not execute when specified number 100 is reached

Ooo it's pretty difficult to examine what happens. You just discribed the behavior again.
It's an internal problem of cmd.exe that I would rather call a bug because ...
- Breaking works for other kinds of FOR loops but FOR /L. That implies that breaking a loop was intended by the developer.
- Breaking a FOR /L loop does't work properly even if it mostly looks like it would. We don't get any warning or error message though. That's what let me believe the developer wasn't aware of his fault.

Regards
aGerman

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How can I set a value to %%variable in the for /L loop?

#19 Post by einstein1969 » 18 Sep 2014 15:39

the only way i know for break a for /L loop is generating an error. But this is without sense.

Jeb introduce this trick for break an infinite for /L loop (1 0 1) for examples.

I used for speedup a code.

Don't work for nested loops. link

einstein1969

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How can I set a value to %%variable in the for /L loop?

#20 Post by Aacini » 19 Sep 2014 21:01

@bars143,

All your questions and tests about the behaviour of a GOTO or CALL command placed inside a FOR /L loop had been discussed with detail in the threads indicated by aGerman above:

aGerman wrote:...

Of course we had some discussions about it. I started one of them
http://www.dostips.com/forum/viewtopic.php?t=2707
Also see
viewtopic.php?t=3487

Regards
aGerman


Antonio

Post Reply