Skip to content

Instantly share code, notes, and snippets.

@davehague
Created September 24, 2024 17:13
Show Gist options
  • Save davehague/19117e9f15fa023f6f6b4b1c0a4fd734 to your computer and use it in GitHub Desktop.
Save davehague/19117e9f15fa023f6f6b4b1c0a4fd734 to your computer and use it in GitHub Desktop.
Print a nicely formatted file tree in Powershell

Often I'll find it helpful to give a LLM context of my file structure for coding projects, so I co-wrote this script to only grab the relevant files and output them in a nicely formatted tree.

As you can see, it ignores common directories in a VS code (Nuxt and Vue) project and in a Pycharm (Python) project. It also only grabs files with approved extensions.

function Print-Tree {
    param (
        [string]$Path,
        [string]$Indent = ""
    )

    $excluded = @('.nuxt', 'node_modules', '.venv', 'venv', '.idea', '__pycache__')
    $extensions = @(".py", ".txt", ".md", ".ini", ".ts", ".json", ".vue", ".yaml", ".env")

    
    Get-ChildItem -Path $Path | Where-Object { $_.Name -notin $excluded } | ForEach-Object {
        if ($_.PSIsContainer) {
            Write-Output "$Indent|-- $($_.Name)"
            Print-Tree -Path $_.FullName -Indent "$Indent|   "
        } else {                                           # Use this line for all extensions
        # } elseif ($extensions -contains $_.Extension) {  # Use this line to only include whitelisted extensions

            Write-Output "$Indent|-- $($_.Name)"
        }
    }
}

# Call the function with the initial path
Print-Tree -Path "C:\Users\david\source\davehague-site"

It's been really helpful to have this script to give the LLM extra context about my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment