Skip to content

Instantly share code, notes, and snippets.

@5E7EN
Created February 21, 2024 21:36
Show Gist options
  • Save 5E7EN/efb412c56b2b70aa38fa50b56593a9ba to your computer and use it in GitHub Desktop.
Save 5E7EN/efb412c56b2b70aa38fa50b56593a9ba to your computer and use it in GitHub Desktop.
Simple powershell script to check if the text contained in a file is within the limits for ChatGPT prompt. Supports -Truncate flag to optionally fit the text.
param (
[string]$FilePath, # Path to the text file
[int]$Limit = 4096, # Default limit set to 4096 characters
[switch]$Truncate # Switch to indicate if truncation should be performed
)
function Get-FileContent {
param (
[string]$Path
)
if (Test-Path $Path) {
Get-Content $Path -Raw
}
else {
Write-Error "File not found: $Path"
exit
}
}
function CheckAndTruncateText {
param (
[string]$Text,
[int]$MaxLength,
[bool]$ShouldTruncate
)
$length = $Text.Length
Write-Host "Text length: $length characters"
if ($length -le $MaxLength) {
Write-Host "Text is within the limit."
}
elseif ($ShouldTruncate) {
$truncatedText = $Text.Substring(0, $MaxLength)
Write-Host "Text has been truncated to fit the limit."
Write-Host $truncatedText
}
else {
Write-Host "Text exceeds the limit. Consider -Truncate to fit."
}
}
$text = Get-FileContent -Path $FilePath
CheckAndTruncateText -Text $text -MaxLength $Limit -ShouldTruncate $Truncate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment