iqover9000
iqover9000

Reputation: 653

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?

Upvotes: 50

Views: 82571

Answers (7)

Newt
Newt

Reputation: 887

  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

Upvotes: 14

Jesse Chen
Jesse Chen

Reputation: 529

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

Upvotes: 27

Epervier 666
Epervier 666

Reputation: 117

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

Upvotes: 5

J. Scott Elblein
J. Scott Elblein

Reputation: 4253

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.

Upvotes: 4

SF Q
SF Q

Reputation: 41

Example 3 levels:

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

Upvotes: 4

Dariusz Sikorski
Dariusz Sikorski

Reputation: 4407

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.

Upvotes: 26

Willard
Willard

Reputation: 399

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

Upvotes: 5

Related Questions