From e085d9dadc2d7bb317e35d8c6b3dd098cd952748 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Wed, 19 Mar 2025 10:59:03 -0600 Subject: [PATCH] Create RenameInvoice.ps1 First try --- RenameInvoice.ps1 | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 RenameInvoice.ps1 diff --git a/RenameInvoice.ps1 b/RenameInvoice.ps1 new file mode 100644 index 0000000..4f65572 --- /dev/null +++ b/RenameInvoice.ps1 @@ -0,0 +1,54 @@ +# Ensure 'pdftotext.exe' is available in your PATH or specify full path here +$pdftotextPath = "pdftotext.exe" + +# Get all PDF files in the script's current directory +$pdfFiles = Get-ChildItem -Path $PSScriptRoot -Filter *.pdf + +foreach ($pdfFile in $pdfFiles) { + # Temporary text file for extracting content + $tempTxt = "$env:TEMP\$(New-Guid).txt" + + # Extract text content from PDF + & $pdftotextPath -layout $pdfFile.FullName $tempTxt + + # Read extracted content + $content = Get-Content $tempTxt + + # Initialize variables + $billToName = $null + $foundBillTo = $false + + # Search line-by-line for the correct 'Bill to' line + for ($i = 0; $i -lt $content.Length; $i++) { + if ($content[$i] -match "Bill to\s*$") { + # Find the next non-empty line after "Bill to" + for ($j = $i + 1; $j -lt $content.Length; $j++) { + if ($content[$j].Trim()) { + $billToName = $content[$j].Trim() + break + } + } + if ($billToName) { break } + } + } + + if ($billToName) { + # Generate new filename + $newFileName = "$billToName 2025 calendar ad invoice.pdf" + + # Check if the target file already exists + if (-not (Test-Path "$($pdfFile.DirectoryName)\$newFileName")) { + # Rename file + Rename-Item -Path $pdfFile.FullName -NewName $newFileName -Verbose + } + else { + Write-Warning "File '$newFileName' already exists. Skipping rename." + } + } + else { + Write-Warning "Could not find 'Bill to' in file '$($pdfFile.Name)'. Skipping file." + } + + # Clean up temporary file + Remove-Item $tempTxt -Force +}