好的 - 這應該做到這一點。它需要一個參數(根文件夾)。
@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