Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created May 4, 2024 13:48
Show Gist options
  • Save dfinke/d2bba1b320f332d109bb6b7785f93548 to your computer and use it in GitHub Desktop.
Save dfinke/d2bba1b320f332d109bb6b7785f93548 to your computer and use it in GitHub Desktop.
Concatenate a directory full of files into a single prompt for use with LLMs
<#
.SYNOPSIS
Concatenate a directory full of files into a single prompt for use with LLMs
.DESCRIPTION
Takes one or more paths to files or directories and outputs every file, recursively, each one preceded with its filename like this:
path/to/file.py
----
Contents of file.py goes here
---
path/to/file2.py
---
...
.PARAMETER Path
Specifies the path of the files to be processed.
.EXAMPLE
Invoke-FilesToPrompt -Path "C:\MyFiles"
This example invokes the Invoke-FilesToPrompt function to process files in the "C:\MyFiles" directory.
.EXAMPLE
Invoke-FilesToPrompt -Path "C:\MyFiles\*.md"
This example invokes the Invoke-FilesToPrompt function to process all Markdown files in the "C:\MyFiles" directory.
.EXAMPLE
Invoke-FilesToPrompt -Path "C:\MyFiles\*.md", "C:\MyOtherFiles\*.md"
This example invokes the Invoke-FilesToPrompt function to process all Markdown files in the "C:\MyFiles" directory.
#>
function Invoke-FilesToPrompt {
[CmdletBinding()]
param (
$Path
)
foreach ($item in $Path) {
if (!(Test-Path $item) ) {
Write-Host "$item does not exist." -ForegroundColor Red
continue
}
Write-Host "Processing $item" -ForegroundColor Green
foreach ($targetItem in Get-ChildItem $item -Recurse) {
$content = Get-Content $targetItem.FullName -raw
@"
$($targetItem.FullName)
---
$content
"@
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment