0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでtreeコマンド風に出力する

Posted at

概要

  • パワーシェルのコマンドが使いづらいので、コピペできるように置いておく
  • ディレクトリを除外できないのはきつい、、

コマンド

以下のコマンドをコピペしてやれば関数を呼び出して利用可能である
多分ファイル用意してエイリアスで実行も可能なはず

function Show-TreeView {
    param (
        [string[]]$Exclude = @('node_modules', '.history', '.old'),
        [string]$Path = ".",
        [int]$CurrentDepth = 0
    )
    
    $esc = [char]27
    $blue = "$esc[34m"
    $reset = "$esc[0m"
    
    $items = Get-ChildItem -Path $Path | Where-Object { 
        $_.Name -notmatch ($Exclude -join '|')
    }
    
    $lastItem = $items | Select-Object -Last 1
    
    foreach ($item in $items) {
        $isLast = $item.FullName -eq $lastItem.FullName
        $prefix = if ($CurrentDepth -gt 0) {
            "    " * ($CurrentDepth - 1) + $(if ($isLast) { "`--" } else { "|--" })
        } else { "" }
        
        $name = if ($item.PSIsContainer) {
            "$blue[+] $($item.Name)$reset"
        } else {
            "[-] $($item.Name)"
        }
        
        Write-Output "$prefix$name"
        
        if ($item.PSIsContainer) {
            Show-TreeView -Path $item.FullName -Exclude $Exclude -CurrentDepth ($CurrentDepth + 1)
        }
    }
}

実行

Show-TreeView -Path "." -Exclude @("node_modules", ".git", "bin",".history",".old")

出力

[+] .private
|--[+] aa
    --[-] bb
|--[-] sample.json
[-] package-lock.json
[-] package.json
[-] sample.js

感想

シンプルだけど実用に耐えるので、いい感じにできた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?