Set Recursive-Depth for dir command in dos

后端 未结 4 389
情书的邮戳
情书的邮戳 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:03

    This is a little enhancement over the solution of dbenham (and elady). It indents output according to depth. It significantly increases readability.

    @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
    set "indent=."
    if %currentLevel% lss %maxLevel% (
      for /d %%F in (*) do (
        for /l %%i in (1,1,%currentLevel%) do echo|set /p=%indent%
        if %currentLevel% geq %minLevel% echo %%~fF
        set /a currentLevel+=1
        call :procFolder "%%F"
        set /a currentLevel-=1
      )
    )
    popd
    

    One can set the indentation character in set "indent ...

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-31 05:19

    I'm sure it is possible to write a complex command that would list n levels of directories. But it would be hard to remember the syntax and error prone. It would also need to change each time you want to change the number of levels.

    Much better to use a simple script.

    EDIT 5 Years Later - Actually, there is a simple one liner that has been available since Vista. See my new ROBOCOPY solution.

    Here is a batch solution that performs a depth first listing. The DIR /S command performs a breadth first listing, but I prefer this depth first format.

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

    The breadth first version is nearly the same, except it requires an extra FOR loop.

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

    Both scripts expect two arguments:

    arg1 = the path of the root directory to be listed

    arg2 = the number of levels to list.

    So to list 3 levels of the current directory, you could use

    listDirs.bat . 3
    

    To list 5 levels of a different directory, you could use

    listDirs.bat "d:\my folder\" 5
    
    0 讨论(0)
  • 2020-12-31 05:20

    After all this time (5 years), and I just now stumble on a simple command line one liner that has been available all along. ROBOCOPY has been a standard Windows utility since Vista, and is available to XP via the Windows Resource Kit.

    robocopy . . /l /s /njh /njs /ns /lev:4 >c:\temp\dir_list.txt
    

    Explanation

        /L :: List only - don't copy, timestamp or delete any files.
        /S :: copy Subdirectories, but not empty ones.
      /NJH :: No Job Header.
      /NJS :: No Job Summary.
       /NS :: No Size - don't log file sizes.
    /LEV:n :: only copy the top n LEVels of the source directory tree.
    

    The /lev:n option includes the root in the count, and you want 3 subdirectory levels, which is why I added 1 to the value.

    Processing further

    The output is not perfect in that the root folder is included in the output, and each path includes fixed width leading whitespace. You can conveniently eliminate the root path as well as the leading whitespace with FOR /F.

    (for /f "skip=2 tokens=*" %A in ('robocopy . . /l /s /njh /njs /ns /lev:4') do @echo %A) >c:\temp\dir_list.txt
    

    The ROBOCOPY output includes an initial blank line, which is why the skip must be 2 instead of 1.

    Each path will end with \. I like that feature because it makes it obvious that we are listing folders and not files. If you really want to eliminate the trailing \, then you can add an extra FOR.

    (for /f "skip=2 tokens=*" %A in ('robocopy . . /l /s /njh /njs /ns /lev:4') do @for %B in ("%A.") do @echo %~fB) >c:\temp\dir_list.txt
    

    But the command is becoming a bit ungainly to type. It should be easy to incorporate this technique into a utility batch file that takes the root path and level as arguments. You must remember to double the percents if you put the command within a batch script.

    0 讨论(0)
提交回复
热议问题