Reputation: 653
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
Reputation: 887
Try to download WSL in your Windows system.
In your command prompt:
bash
Then you can use Linux commands, this example is for 3 levels deep:
tree -L 3
Upvotes: 14
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
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
Reputation: 4253
If you use WSL you can also export the result straight to the Windows clipboard
tree
installed: sudo apt install tree
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
Reputation: 41
Example 3 levels:
tree|findstr /v /r /c:"^........│" /c:"^│....... " /c:"^ ....... "
Upvotes: 4
Reputation: 4407
Since I didn't found complete answer here. Here it is:
Windows CMD doesn't support -L
depth levels.
../../cygdrive/c/myFolder
.tree -L 3 >result.txt
.Upvotes: 26
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