Set Recursive-Depth for dir command in dos

后端 未结 4 476
情书的邮戳
情书的邮戳 2020-12-31 04:55

I\'m currently using the following command to list some directories:

dir /b /s /AD > c:\\temp\\dir_list.txt

This gives me almost the lis

4条回答
  •  再見小時候
    2020-12-31 05:18

    Here is a solution which based on the depth first listing solution of @dbenham,
    and enables to set minimum level as well.

    @echo off
    setlocal
    set currentLevel=0
    set maxLevel=%2
    if not defined maxLevel set maxLevel=1
    set minLevel=%3
    if not defined minLevel set minLevel=0
    
    :procFolder
    pushd %1 2>nul || exit /b
    if %currentLevel% lss %maxLevel% (
      for /d %%F in (*) do (
        if %currentLevel% geq %minLevel% echo %%~FF
        set /a currentLevel+=1
        call :procFolder "%%F"
        set /a currentLevel-=1
      )
    )
    popd
    

    To set the minimum level just provide it as 3rd parameter.
    For example: to list from level 2 to level 5, you could use

    listDirs.bat target_path 5 2
    

    Alternatively, you can list from the base level by leaving this parameter empty

    listDirs.bat target_path 5
    

提交回复
热议问题