【问题标题】:Recursively Echo Relative Path In A Batch批量递归回显相对路径
【发布时间】:2014-07-18 18:22:17
【问题描述】:
我看过很多类似的帖子,但没有一个回答我的问题,或者他们根本不适合我。我正在尝试遍历一个目录并回显该目录中所有文件的相对路径。
我的目录:
- Name
- TestA
* Subfolder
- Test.txt
* Test2.txt
* Test3.txt
- TestB
* Test4.txt
我希望它输出什么:
名称/TestA/子文件夹
名称/测试A
名称/测试A
名称/测试B
我试过这个帖子:batch programming - get relative path of file,但它只适用于 Subfolder 的情况,即使这样,它也会切断 Subf 之后的所有内容。
请帮忙!
【问题讨论】:
标签:
batch-file
echo
relative-path
directory
【解决方案1】:
批处理文件
@echo off
setlocal enabledelayedexpansion
set ParentPath=C:\Temp
for /F "usebackq delims=" %%I in (`dir "%ParentPath%\Name\*.txt" /ON /B /S`) do (
rem Get drive and path of found text file.
set "RelativePath=%%~dpI"
rem Remove the path of parent folder from path of file.
set "RelativePath=!RelativePath:%ParentPath%=!"
rem Remove the backslash at beginning and end of the remaining path.
set "RelativePath=!RelativePath:~1,-1!"
rem Replace every backslash by a slash character.
set "RelativePath=!RelativePath:\=/!"
echo !RelativePath!
rem echo is the relative path of file
rem echo %%~nxI
echo.
)
endlocal
输出
Name/TestA
Name/TestA
Name/TestA/Subfolder
Name/TestB
如您所见,相对路径的顺序与您的要求不同。
顺序对你来说重要吗?