问题描述
关于Windows批处理文件:有没有办法来列出某个目录中的所有文件(或所有特定类型的)及其子目录,包括相对于当前(或搜索),列表中的目录的路径?
Concerning Windows Batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?
例如,如果我想与他们的完整路径在当前目录下的所有txt文件和子目录,我可以做
For example, if I want all the txt files in the current directory and subdirectories with their full paths, I can do
for /r . %%g in (*.txt) do echo %%g >> C:\temp\test.txt
或
dir *.txt /b /s >> C:\temp\test.txt
和我会得到类似
C:\test\Doc1.txt
C:\test\subdir\Doc2.txt
C:\test\subdir\Doc3.txt
如果我
for /r . %%g in (*.txt) do echo %%~nxg >> C:\temp\test.txt
我会像
Doc1.txt
Doc2.txt
Doc3.txt
但我真正想要的是:
But what I really want is:
Doc1.txt
subdir\Doc2.txt
subdir\Doc3.txt
这可能吗?
如果我的职位是太混乱了:我基本上要List使用相对路径到当前目录,只适用于Windows递归在Linux中的CLI文件。
If my post is too confusing: I basically want "List files recursively in Linux CLI with path relative to the current directory", just for Windows.
推荐答案
您可以简单地得到当前目录的字符长度,并将其从您的绝对列表的
You could simply get the character length of the current directory, and remove them from your absolute list
setlocal EnableDelayedExpansion
for /L %%n in (1 1 500) do if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
setlocal DisableDelayedExpansion
for /r . %%g in (*.log) do (
set "absPath=%%g"
setlocal EnableDelayedExpansion
set "relPath=!absPath:~%len%!"
echo(!relPath!
endlocal
)
这篇关于批处理文件:列出与相对路径的目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!