The simplest (but not the fastest) way to iterate a directory tree and list relative file paths is to use FORFILES.
forfiles /s /m *.txt /c "cmd /c echo @relpath"
The relative paths will be quoted with a leading .\
as in
".\Doc1.txt"
".\subdir\Doc2.txt"
".\subdir\Doc3.txt"
To remove quotes:
for /f %%A in ('forfiles /s /m *.txt /c "cmd /c echo @relpath"') do echo %%~A
To remove quotes and the leading .\
:
setlocal disableDelayedExpansion
for /f "delims=" %%A in ('forfiles /s /m *.txt /c "cmd /c echo @relpath"') do (
set "file=%%~A"
setlocal enableDelayedExpansion
echo !file:~2!
endlocal
)
or without using delayed expansion
for /f "tokens=1* delims=\" %%A in (
'forfiles /s /m *.txt /c "cmd /c echo @relpath"'
) do for %%F in (^"%%B) do echo %%~F