您可以(错误地)使用xcopy command,它可以列出要复制的文件的相对路径,并且具有选项/L 不复制而只列出不复制的文件。
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%"
这将产生如下输出:
.\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)
目标可以是现有且可访问的驱动器上的任意目录路径,该驱动器不位于源目录树中。
请注意,这仅适用于文件,但不适用于目录路径。
要删除摘要行 … File(s) 让 findstr 过滤掉它:
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 "^.:"
或者使用find 过滤掉这些行:
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 ":"
要删除.\ 或驱动器前缀,请使用for /F 捕获xcopy 输出并使用适当的分隔符:
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
)
如何对驱动器前缀做同样的事情应该很明显:
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
)
这是相关的示例输出:
Doc1.txt
subdir\Doc2.txt
subdir\Doc3.txt