'CMD tree command limit number of levels

How do I limit the number of levels that the tree command goes through in Windows? I need to output the results to a text file for work, but because by default the tree command lists every single directory under the one you ran the command in, the output I'm getting is over 44,000 lines long, which isn't helpful at all for my work. How do I restrict it to listing just the first couple levels?



Solution 1:[1]

Since I didn't found complete answer here. Here it is:

Windows CMD doesn't support -L depth levels.

  1. Install CygWin https://www.cygwin.com.
  2. In Cygwin make sure you pick Utilities / Tree package installed.
  3. Open CygWin and navigate to your folder, like cd ../../cygdrive/c/myFolder.
  4. List tree structure and save as result.txt tree -L 3 >result.txt.

Solution 2:[2]

Actually, the tree command in DOS and Windows does not have the option for specifying the directory level that the command goes through. You can refer to the documentation of tree on Microsoft Docs.

But you can use Git Bash instead. This tool is provided when you install Git for Windows. So in this way, you can use the command that @Zhengquan Feng mentioned which was a deleted answer.

tree -L 3 // three levels show

Solution 3:[3]

  1. Try to download WSL in your Windows system.

  2. In your command prompt:

    bash

  3. Then you can use Linux commands, this example is for 3 levels deep:

    tree -L 3

Solution 4:[4]

You can try this project on Windows :

https://github.com/MrRaindrop/tree-cli

Usage:

use -l levelNumber to specify the path level:

treee -l 2

Solution 5:[5]

If you use WSL you can also export the result straight to the Windows clipboard

  • From the WSL Cline, if you don't already have tree installed: sudo apt install tree
  • Then: tree -L 5 | clip.exe (replace 5 with however many levels deep you want)

That'll bypass saving it to a txt file and go straight to your clipboard for quick pasting into forums, etc.

Solution 6:[6]

Example 3 levels:

tree|findstr /v /r /c:"^........?" /c:"^?....... " /c:"^ ....... "

Solution 7:[7]

I know the question is old, but here in Powershell :

(I know, it needs a little cleaning, hiding variables for the end user, etc... but it works without installing anything)

$sb = New-Object System.Text.StringBuilder

Function Get-Tree {
    Param ([String]$Path, [Int]$LevelMax, [Int]$Level = 0, [Bool]$LastOfTheList=$true, [String]$Lead="")

    if ($Level -eq 0) {$sb.AppendLine($Path)}
    if ($Level -eq $LevelMax) { Return }

    $Lead = if($LastOfTheList){"$Lead   "}else{"$Lead$([char]0x2502)  "}

    [Array]$Liste = $Path | Get-ChildItem -Directory

    For($x=0;$x -lt $Liste.count;$x++) {
        $item = $Liste[$x]
      
        if ($x -eq $Liste.Count-1) { 
            $sb.AppendLine("$($Lead)$([char]0x2514)$([char]0x2500)$([char]0x2500)$($item.Name)") | Out-Null 
            Get-Tree -Path $item.Fullname -level ($level +1) -LevelMax $LevelMax -Lead $Lead -LastOfTheList $true
        }
        else { 
            $sb.AppendLine("$($Lead)$([char]0x251c)$([char]0x2500)$([char]0x2500)$($item.Name)") | Out-Null 
            Get-Tree -Path $item.Fullname -level ($level +1) -LevelMax $LevelMax -Lead $Lead -LastOfTheList $false
        }
    }
}

Get-Tree -Path "MyPath" -LevelMax 3
$sb.ToString() | Out-File "MyOutputFile.txt" -Encoding UTF8

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 copolii
Solution 2 Jeremy Thompson
Solution 3 Jeremy Thompson
Solution 4 Willard
Solution 5 J. Scott Elblein
Solution 6 SF Q
Solution 7 Epervier 666