Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Problem with %_do_loop in nested do loops

Apr
393
13
TCC 24.02.50 x64 Windows 10 [Version 10.0.18362.239]
TCC Build 50 Windows 10 Build 18362

According to the help the internal variable %_do_loop contains "The number of times the current DO loop has been executed". I read that to mean that if Do loops are nested, then %_do_loop will return the correct value for the "currently active" loop. So I would expect this test code:

Code:
@echo off
do 3
  echo %_do_loop
  do 5
   echos %_do_loop` `
  enddo
  echo ``
  echo %_do_loop
  echo ``
enddo

to return this:
Code:
1
1 2 3 4 5
1

2
1 2 3 4 5
2

3
1 2 3 4 5
3

However, what it actually returns is:
Code:
1
1 2 3 4 5
6

2
1 2 3 4 5
6

3
1 2 3 4 5
6

It seems that on return from the nested Do, the internal variable is left with its last value from the nested loop.
 
%_do_loop is a global variable; it is not intended to be used in nested DO loops. It's being updated at the top of the loop, so when you test after the inner DO loop it still has the value from the inner loop.
OK, thanks Rex.

Your statement is the exact opposite of what the help says about %_do_loop (and indeed the other three DO variables), here's a quote from the DO help page:
The number of directories traversed (with /S) for the current DO loop. (I.e., nested DO's each have their own _do_dirs, _do_files, _do_errors, and _do_loop.)

So if it's WAD that one or more of those variables are global and don't work in nested loops could I request that you update the help to make it clear please?
 

Similar threads

Back
Top