@echo off setlocal set /a "n=0, limit=5" >"testfile1.txt" ( for /f "eol=: delims=" %%F in ('dir /od /b abc*.*') do ( echo %%F 2>nul set /a "n+=1, 1/(limit-n)"||goto :break ) ) :break
I intentionally divide by 0 to determine when the limit is reached. I could just use the IF statement instead, but that would require a delayed extension, and a slow extension would distort the filename containing ! . The correct solution with expansion delay should turn on and off slow expansion within the loop.
source share