好的 - 这应该做到这一点。它需要一个参数(根文件夹)。
@ECHO OFF
SET root=%1
REM Get the length of the root, to make the path relative later.
REM See http://stackoverflow.com/questions/5837418/how-do-you-get-the-string-length-in-a-batch-file.
ECHO %root%>x&FOR %%? IN (x) DO SET /A rootlength=%%~z? - 1&del x
for /F "tokens=*" %%G in ('DIR %1 /AD /S /B') do (
CALL :PrintDirectory "%%G" %rootlength%
)
GOTO :eof
:PrintDirectory
REM %1 Path to the folder
REM %2 Length of root string.
REM See http://www.dostips.com/DtTipsStringManipulation.php#Snippets.LeftString for
REM information on the string manipulation.
@ECHO OFF
SET start=%2
SET absPath=%1
REM Remove the path root by taking the right-hand side of the string.
CALL SET relPath=%%absPath:~%start%,-1%%
ECHO.%relPath%
您可以通过重定向的结果,一个批处理文件执行:
PrintDirectoryStructure.bat c:\MyProject > out.txt
这是否有批办?例如,在Powershell中这将非常简单,但在批处理中非常困难。 – 2012-08-07 07:47:01
是的。仅在批处理文件中,为了简单和兼容性。 – 2012-08-07 07:48:36
你可以调用VBScript吗?再次,这将使事情变得极其简单... – 2012-08-07 08:04:26