diff --git a/Scripts/Powershell/General Purpose/Directory Walker.md b/Scripts/Powershell/General Purpose/Directory Walker.md new file mode 100644 index 0000000..8dda435 --- /dev/null +++ b/Scripts/Powershell/General Purpose/Directory Walker.md @@ -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" +``` \ No newline at end of file