From d03498d118c588e023b3381d30285e90e4f7c5e2 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Wed, 19 Mar 2025 11:02:52 -0600 Subject: [PATCH] Update RenameInvoice.ps1 Use a more flexible format search approach; use regex to match lines containing "Bill to" (allowing extra whitespace). Grab the very next non-empty line. Enhance text search robustness --- RenameInvoice.ps1 | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/RenameInvoice.ps1 b/RenameInvoice.ps1 index 7531e99..b420e51 100644 --- a/RenameInvoice.ps1 +++ b/RenameInvoice.ps1 @@ -1,4 +1,4 @@ -# Ensure 'pdftotext.exe' is available in your PATH or specify the full path here +# 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 @@ -14,13 +14,13 @@ foreach ($pdfFile in $pdfFiles) { # Read extracted content line-by-line $lines = Get-Content $tempTxt - # Initialize variable to store Bill To name + # Initialize Bill To name $billToName = $null - # Find the line with 'Bill to' exactly and take the next non-empty line + # Improved search for the line containing 'Bill to' (allowing whitespace variations) for ($i = 0; $i -lt $lines.Length; $i++) { - if ($lines[$i].Trim() -eq "Bill to") { - # Find the next non-empty line as the company name + if ($lines[$i] -match '^\s*Bill\s+to\s*$') { + # Next non-empty line is the name for ($j = $i + 1; $j -lt $lines.Length; $j++) { $nextLine = $lines[$j].Trim() if ($nextLine -ne "") { @@ -33,21 +33,14 @@ foreach ($pdfFile in $pdfFiles) { } if ($billToName) { - # Clean up billToName (remove invalid filename chars) + # Clean up name (remove invalid filename chars) $cleanName = ($billToName -replace '[\\/:*?"<>|]', '').Trim() - # Generate new filename safely + # Construct new filename safely $newFileName = "$cleanName 2025 calendar ad invoice.pdf" - - # Ensure the filename isn't excessively long - if ($newFileName.Length -gt 200) { - $newFileName = $newFileName.Substring(0, 200) + ".pdf" - } - - # Build full destination path explicitly $destPath = Join-Path -Path $pdfFile.DirectoryName -ChildPath $newFileName - # Check if the file already exists + # Avoid conflicts if (-not (Test-Path $destPath)) { try { Rename-Item -Path $pdfFile.FullName -NewName $newFileName -Verbose -ErrorAction Stop @@ -61,6 +54,6 @@ foreach ($pdfFile in $pdfFiles) { Write-Warning "Could not find 'Bill to' in file '$($pdfFile.Name)'. Skipping file." } - # Clean up temporary file + # Cleanup temporary file Remove-Item $tempTxt -Force }