According to the documentation, "LEAVE exits from the current DO loop and continues with the command following its ENDDO." However, these commands:
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.
Code:
do file in /d"d:\" /a:-d /s "*"
echo file is %_cwd\%file
leave
echo after the leave
enddo
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.
Code:
@ 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.
quit
endiff
:: Set up a test environment.
mkdir /s %dirname\b\c %dirname\bb\cc
cd %dirname
touch /s file1 file2 >& NUL
echo.
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.
echo Testing with this fileset:
tree /f
echo.
:: Run the test.
do file in /d"%dr:\%dirname" /a:-d /s "*"
echo file is %_cwd\%file
leave
echo after the leave
enddo
:: Clean up afterwards.
cd \
del /s /x /y /z %dirname >& NUL
Last edited: