Directory Walker

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.

function Export-Tree {
    param (
        [string]$Path = ".",
        [string]$OutFile = "directory_tree.txt"
    )

    $global:TreeLines = @()
    $global:IgnoreList = @(
        ".git",
        "Dependencies"
    )

    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"