批处理文件以列出具有父目录的文件和文件夹

时间:2014-02-07 20:19:55

标签: file batch-file directory parent

我见过来自不同人士的脚本,他们建议使用下面的代码:

    @echo off
    setlocal disableDelayedExpansion
    pushd %1
    set "tab=   "
    set "indent="
    call :listFolder >report.txt
    exit /b

    :listFolder
    setlocal
    set "indent=%indent%%tab%"

    for /d %%F in (*) do (
      echo %indent%%%F
      pushd "%%F"
      call :listFolder
      popd
    )
    for %%F in (*) do echo %indent%%%F

exit /b

输出:

Folder 1
   Subfolder 1
      Filename 1
   Subfolder 2
      Filename 1
Folder 2
   Subfolder 1
      Filename 2
      Filename 2

这种方法很有效但我想知道是否有办法做同样的想法,但让父文件夹用标签分隔。

Folder 1
Folder 1    Subfolder 1
Folder 1    Subfolder 1     Filename 1
Folder 1    Subfolder 2
Folder 1    Subfolder 2     Filename 1
Folder 2
Folder 2    Subfolder 1
Folder 2    Subfolder 1     Filename 2
Folder 2    Subfolder 1     Filename 2

2 个答案:

答案 0 :(得分:0)

@echo OFF
SETLOCAL
pushd %1
set "tab=/"
SET "currdir=%cd%"
call :listFolder >report.txt
popd
GOTO :eof

:listFolder
setlocal
for /d %%a in (*) do (
 SET "name=%%~fa"
 SETLOCAL ENABLEDELAYEDEXPANSION
 SET name=!name:%currdir%=!
 SET name=!name:\=%tab%!
 ECHO !name!
 ENDLOCAL
 pushd "%%a"
 call :listFolder
 popd
)
for %%a in (*) do (
 SET "name=%%~fa"
 SETLOCAL ENABLEDELAYEDEXPANSION
 SET name=!name:%currdir%=!
 SET name=!name:\=%tab%!
 ECHO !name!
 ENDLOCAL
)

GOTO :eof

有趣的运动。

基本上,报告然后变成“做一个dir/s/b列表,但用{kbd> Tab 替换\并省略当前目录” - 但是这个简单的方案没有产生相同的序列...

(我还用/替换了标签以便于查看,标签不是特别明显而且全部 - 只需选择适合的角色......

答案 1 :(得分:0)

另一个版本:

@echo off
setlocal disableDelayedExpansion
set "pwd=%~1"
if "%pwd%"=="" set "pwd=%cd%"

pushd "%pwd%"
set "indent=   "
call :listFolder1 "%pwd%"
exit /b

:listFolder1 %1
setlocal
set curdir=%~1
for /d %%F in (*) do (
    echo %curdir%%indent%%%F
    for %%S in (*) do echo %curdir%%indent%%%F%indent%%%S
    pushd %%F
    call :listFolder1 "%curdir%%indent%%%F"
    popd
)