方法一:CMD 自带 tree 命令(最简单,无需安装)
1. 打开 CMD(在 test 文件夹的上级目录地址栏输入 `cmd` 回车)
2. 执行:tree /F test
- `/F` 表示同时显示文件和文件夹
- 不加 `/F` 只显示文件夹
输出效果类似:
test
├── archive
├── .gitattributes
├── .gitignore
├── .python-version
...
如果想保存到文件:tree /F test > tree.txt
方法二:PowerShell 生成 Unicode 树形图
把下面脚本保存为 Show-Tree.ps1,在 test 文件夹上级目录运行:
function Show-Tree {
param([string]$Path = ".", [int]$Depth = 0, [string]$Prefix = "")
$items = Get-ChildItem -Path $Path -Force | Sort-Object { -not $_.PSIsContainer }, Name
$count = $items.Count
for ($i = 0; $i -lt $count; $i++) {
$item = $items[$i]
$isLast = ($i -eq $count - 1)
$connector = if ($isLast) { "└── " } else { "├── " }
Write-Output ($Prefix + $connector + $item.Name)
if ($item.PSIsContainer) {
$newPrefix = $Prefix + $(if ($isLast) { " " } else { "│ " })
Show-Tree -Path $item.FullName -Depth ($Depth + 1) -Prefix $newPrefix
}
}
}
Write-Output "test/"
Show-Tree -Path "test"运行:powershell -ExecutionPolicy Bypass -File Show-Tree.ps1
输出就是标准的 `├──` / `└──` / `│` 格式。
方法三:npm 工具 treer(推荐,功能最全)
安装Node.js
npm install -g treer
treer -d test保存文件
treer -d test > tree.txt如果在当前目录里面
treer > tree.txt还可以忽略特定文件:
treer -d test -i "node_modules|.git"总结:临时用一下选 方法一;Unicode 格式选 方法二;经常需要生成目录树选 方法三。