Powershell模拟实现Linux下的tree命令
代码
环境: P o w e r s h e l l 7 Powershell\ 7 Powershell 7
function Get-Tree {
param(
[string]$directory = ".",
[int]$d = 1,
[switch]$f
)
$absolutePath = Resolve-Path -Relative $directory
Write-Host $absolutePath
function Recurse-Tree {
param(
[string]$currentDir,
[int]$currentDepth
)
$indent = " " * $currentDepth
$items = Get-ChildItem -Path $currentDir
foreach ($item in $items) {
if ($item.PSIsContainer -or $f) {
Write-Host ("{0}|-- {1}" -f $indent, $item.Name)
if ($item.PSIsContainer -and ($currentDepth -lt $d -or $d -eq -1)) {
Recurse-Tree -currentDir $item.FullName -currentDepth ($currentDepth + 1)
}
}
}
}
Recurse-Tree -currentDir $directory -currentDepth 0
}
New-Alias -Name tree -Value Get-Tree
将上述代码拷贝到"C:\Users\${username}\Documents\PowerShell\profile.ps1"
如果是 P o w e r s h e l l 5 Powershell\ 5 Powershell 5:
拷贝到"C:\Users\${username}\Documents\WindowsPowerShell\profile.ps1"
效果
运行:
tree D:\py-projects -d 1
打印效果
D:\py-projects
|-- CartPole-actor-critic-based
|-- __pycache__
|-- .vscode
|-- A2C
|-- PPO
|-- REINFORCE
|-- result
|-- checkpoints
|-- cliff_walk
|-- crack_detection_mp
|-- .vscode
|-- backend
|-- model_deploy
|-- mp-weixin
|-- CustomEnv
|-- __pycache__
|-- .vscode
|-- result
|-- deep-in-python-use
|-- faster-rcnn-deploy
|-- .vscode
|-- checkpoints
|-- datasets
|-- flagged
|-- mmdeploy
|-- mmdeploy_model
|-- mmdetection
|-- GridWorld-value-based
|-- .idea
|-- .vscode
|-- A2C
|-- backup
|-- DQN
|-- REINFORCE
|-- gym-examples
|-- gym_examples
|-- open-resume
|-- public
|-- src
|-- Pendulum-actor-critic-based
|-- .vscode
|-- bkp
|-- DDPG
|-- result
|-- SAC
|-- TD3
|-- pycharm-projects
|-- .idea
|-- QQFlacMusicDownloader
|-- .idea
|-- flaskSystem
|-- md
|-- music
|-- WebSourceCode
|-- reinforcejs
|-- agentzoo
|-- external
|-- img
|-- lib
|-- RL_test
|-- __pycache__
|-- RL-notes
|-- stitp_deploy
|-- checkpoints
|-- mmdeploy
|-- mmdeploy_model
|-- mmdeploy-1.2.0-windows-amd64-cuda11.3
|-- mmdetection
|-- mmpretrain
|-- work_dir