LEAVE in a DO /S fileset loop only leaves one subdirectory

May 23, 2010
According to the documentation, "LEAVE exits from the current DO loop and continues with the command following its ENDDO." However, these commands:
  do file in /d"d:\" /a:-d /s "*"
    echo file is %_cwd\%file
    echo after the leave
show that that is not true when processing subdirectories. In that case, processing of the loop is performed once (for the first file!) in each subdirectory. In each case, the LEAVE is executed, but sends control back to the top of the loop. Control goes to the line after the ENDDO only when one file in each of all the subdirectories has been processed. Adding a number to the LEAVE will produce varying results.

Here's a .btm file you can use to test in a smaller environment than your total disk drive. It builds a test area, runs the do loop, then cleans up after itself. If you already have a directory D:\A, change the code to use a different root directory.
@ echo off
  :: Pick a root directory name.
  set dr=d
  set dirname=a
  cdd %dr:\
  iff exist %dirname then
    echo There is already a file or directory named %dr:\%dirname.
    echo Change this code to create a root directory that you don't have.

  :: Set up a test environment.
  mkdir /s %dirname\b\c %dirname\bb\cc
  cd %dirname
  touch /s file1 file2  >& NUL

  echo Testing this construct:
  echo do file in /d"%dr:\%dirname" /a:-d /s "*"
  echo   echo file is %%_cwd\%%file
  echo   leave
  echo   echo after the leave
  echo enddo

  echo Testing with this fileset:
  tree /f

  :: Run the test.
  do file in /d"%dr:\%dirname" /a:-d /s "*"
    echo file is %_cwd\%file
    echo after the leave

  :: Clean up afterwards.
  cd \
  del /s /x /y /z %dirname >& NUL
Last edited:
May 20, 2008
Syracuse, NY, USA
Here's a simpler one. I reckon it should process only one file but it processes one in each directory.

v:\> do x in /s * ( echo %x & leave )
May 23, 2010
The GOTO command will serve as a workable replacement for LEAVE. And the current behavior of the LEAVE does offer some advantages, if you are really looking to process the first file found in each directory. (Otherwise, it would take a bit of coding to accomplish that objective.) But that's not what the documentation says.


Staff member
May 14, 2008
LEAVE is working as intended in DO. The documentation is incomplete in the case of a DO /S - if you specify /S, a LEAVE will terminate processing that directory and continue with the next one. LEAVE has always behaved that way (by user request); if you want to cancel a DO /S operation you can use GOTO or LEAVE n (LEAVE n isn't recommended if you're nesting DO's and only want to leave the current nesting level).
Similar threads
Thread starter Title Forum Replies Date
D LEAVE quits btm Support 5
old coot LEAVE appears to affect command-line arguments Support 11
Peter Murschall A little problem with LEAVE and COMMENT Support 5
vefatica Fixed LEAVE N not working Support 7
D How to skip files in a fileset? Support 6
samintz How to? Abort on error DO loop Support 10
D How to use multi-line DO to loop through first level directory names Support 5
vefatica ON ERROR terminates DO loop. Support 0
Dick Johnson for ... do loop "wrap around" Support 11
vefatica Modifying a counted DO loop's counter? Support 1
Z [dos] into - for loop - the bp is not hit Support 5
vefatica Piping the output of a DO loop. Support 4
rps For loop internal %_for_files variable? Support 2
D What is the limit on the compound statement length within ( ) of a FOR-loop? Support 5
old coot Proper use of ON ERRORMSG inside a DO WHILE loop? Support 3
H EndLocal in DO Loop Support 2
B How to? redirect a bunch of loop Support 4
Mike Hall Unexpected "Variable loop" in IF condition Support 10
C FOR loop with nested IF / IFF Support 9
D Strange issue with FOR loop Support 15
nickles WAD Variable name gets changed within FOR loop Support 2
M tcmd crashes in this recursive "for /f" loop Support 4
G Fixed Slow FOR loop Support 2
S WAD %_do_errors does not report errors when loop control variable type is not directory entry Support 1
mikea 'Loop' on closing TC Support 6
Roedy Loop anomaly Support 6
R nested for loop question Support 3
P Need to use a regex in a "for" loop. Support 54

Similar threads