You could (mis-)use the xcopy
command1 that can list relative paths of files to be copied and that features the option /L
to not copy but just list files that would be copied without.
rem // First change into the target directory:
cd /D "C:\test"
rem // Now let `xcopy` list files relative to the current directory preceded with `.\`:
xcopy /L /S /Y /R /I ".\*.txt" "%TEMP%"
rem // Or let `xcopy` list files relative to the current directory preceded with the drive:
xcopy /L /S /Y /R /I "*.txt" "%TEMP%"
This would produce an output like such:
.\Doc1.txt
.\subdir\Doc2.txt
.\subdir\Doc3.txt
3 File(s)
C:Doc1.txt
C:subdir\Doc2.txt
C:subdir\Doc3.txt
3 File(s)
The destination can be an arbitrary directory path on an existing and accessible drive that does not reside within the source directory tree.
Note that this only works with file but not with directory paths.
To delete the summary line … File(s)
let findstr
filter it out:
cd /D "C:\test"
rem // Filter out lines that do not begin with `.\`:
xcopy /L /S /Y /R /I ".\*.txt" "%TEMP%" | findstr "^\.\\"
rem // Filter out lines that do not begin with a drive letter + `:`:
xcopy /L /S /Y /R /I "*.txt" "%TEMP%" | findstr "^.:"
Alternatively use find
to filter such lines out:
cd /D "C:\test"
rem // Filter out lines that do not contain `.\`:
xcopy /L /S /Y /R /I ".\*.txt" "%TEMP%" | find ".\"
rem // Filter out lines that do not contain `:`:
xcopy /L /S /Y /R /I "*.txt" "%TEMP%" | find ":"
To remove the .\
or the drive prefix, capture the xcopy
output with for /F
and use an appropriate delimiter:
cd /D "C:\test"
rem // The 1st token is a `.`, the remaining token string `*` is going to be empty for the summary line:
for /F "tokens=1* delims=\" %%I in ('
xcopy /L /S /Y /R /I ".\*.txt" "%TEMP%"
') do (
rem // Output the currently iterated item but skip the summary line:
if not "%%J"=="" echo(%%J
)
It should be quite obvious how to do the same for the drive prefix:
cd /D "C:\test"
rem // The 2nd token is empty for the summary line and is not even going to be iterated:
for /F "tokens=2 delims=:" %%I in ('
xcopy /L /S /Y /R /I "*.txt" "%TEMP%"
') do (
rem // Simply output the currently iterated item:
echo(%%I
)
This is the related sample output:
Doc1.txt
subdir\Doc2.txt
subdir\Doc3.txt
1) The basic approach derives from this answer by user MC ND.