我希望通过文件夹/子文件夹/等进行递归。 &安培;用动态填充包含文件夹路径的数组。
示例:我有一个名为" A"的文件夹。它有2个子文件夹" B" &安培; " C&#34 ;. " C"有一个子文件夹" D"。所以数组将是:
Folder[01]=A
Folder[02]=A/B
Folder[03]=A/C
Folder[04]=A/C/D
FOR / D命令能否满足我的需要?如果是这样,我该怎么做循环得到的&将它添加到数组?不幸的是必须批量生产。谢谢!
答案 0 :(得分:3)
使用以下批处理文件(MakeFolderArray.cmd):
@echo off
setlocal enabledelayedexpansion
rem get length of %cd% (the current directory)
call :strlen cd _length
set /a _index=1
for /d /r %%a in (*) do (
set _name=%%a
rem remove everything from the drive root up to the current directory,
rem which is _length chars
call set _name=!!_name:~%_length%!!
rem replace \ with /
set _name=!_name:\=/!
set Folder[0!_index!]=!_name!
set /a _index+=1
)
set /a _index-=1
for /l %%i in (1,1,%_index%) do (
echo Folder[0%%i]=!Folder[0%%i]!
)
endlocal
goto :eof
:strLen strVar [rtnVar]
setlocal disableDelayedExpansion
set len=0
if defined %~1 for /f "delims=:" %%N in (
'"(cmd /v:on /c echo(!%~1!&echo()|findstr /o ^^"'
) do set /a "len=%%N-3"
endlocal & if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b
示例:
F:\test>MakeFolderArray
Folder[01]=/A
Folder[02]=/A/B
Folder[03]=/A/C
Folder[04]=/A/C/D
现金:
感谢dbenham来自this answer的strlen
代码(如果字符串包含\
个字符,则会有效)。