Add Scripts/Powershell/General Purpose/Directory Walker.md
This commit is contained in:
50
Scripts/Powershell/General Purpose/Directory Walker.md
Normal file
50
Scripts/Powershell/General Purpose/Directory Walker.md
Normal file
@ -0,0 +1,50 @@
|
||||
**Purpose**:
|
||||
Sometimes you just need a basic script that outputs a pretty directory and file tree. This script offers files and folders to ignore, and outputs a fancy directory tree.
|
||||
|
||||
```powershell
|
||||
function Export-Tree {
|
||||
param (
|
||||
[string]$Path = ".",
|
||||
[string]$OutFile = "directory_tree.txt"
|
||||
)
|
||||
|
||||
$global:TreeLines = @()
|
||||
$global:IgnoreList = @(
|
||||
".git",
|
||||
"Dependencies",
|
||||
"Server",
|
||||
"Agent"
|
||||
)
|
||||
|
||||
function Walk-Tree {
|
||||
param (
|
||||
[string]$Folder,
|
||||
[string]$Prefix
|
||||
)
|
||||
|
||||
$items = Get-ChildItem -Path $Folder -Force | Where-Object {
|
||||
$_.Name -ne "." -and $_.Name -ne ".." -and
|
||||
($global:IgnoreList -notcontains $_.Name)
|
||||
} | Sort-Object PSIsContainer, Name
|
||||
|
||||
$count = $items.Count
|
||||
|
||||
for ($i = 0; $i -lt $count; $i++) {
|
||||
$item = $items[$i]
|
||||
$connector = if ($i -eq $count - 1) { "└── " } else { "├── " }
|
||||
$global:TreeLines += "$Prefix$connector$($item.Name)"
|
||||
if ($item.PSIsContainer) {
|
||||
$newPrefix = if ($i -eq $count - 1) { "$Prefix " } else { "$Prefix│ " }
|
||||
Walk-Tree -Folder $item.FullName -Prefix $newPrefix
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Walk-Tree -Folder $Path -Prefix ""
|
||||
|
||||
$global:TreeLines | Set-Content -Path $OutFile -Encoding UTF8
|
||||
}
|
||||
|
||||
# Run it
|
||||
Export-Tree -Path "." -OutFile "directory_tree.txt"
|
||||
```
|
Reference in New Issue
Block a user