From 779a37cfa8d20a91bb3f5aa0b69c6ea31c06e583 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Wed, 19 Mar 2025 11:08:55 -0600 Subject: [PATCH] Update RenameInvoice.ps1 Updated the script to perform a full-text regex match --- RenameInvoice.ps1 | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/RenameInvoice.ps1 b/RenameInvoice.ps1 index 73b9928..df42311 100644 --- a/RenameInvoice.ps1 +++ b/RenameInvoice.ps1 @@ -11,27 +11,13 @@ foreach ($pdfFile in $pdfFiles) { # Extract text content from PDF & $pdftotextPath -layout $pdfFile.FullName $tempTxt - # Read extracted content line-by-line - $lines = Get-Content $tempTxt + # Read extracted content as a single string + $content = Get-Content $tempTxt -Raw - # Initialize Bill To name - $billToName = $null + # More robust method: find "Bill to" and capture next meaningful line + if ($content -match "Bill to\s*(?:\r?\n)+\s*(.+)") { + $billToName = $matches[1].Trim() - # More robust method: look for "Bill to" line and grab the next meaningful line - for ($i = 0; $i -lt $lines.Length; $i++) { - if ($lines[$i].Trim() -match '^Bill\s+to$') { - for ($j = $i + 1; $j -lt $lines.Length; $j++) { - $nextLine = $lines[$j].Trim() - if (![string]::IsNullOrWhiteSpace($nextLine)) { - $billToName = $nextLine - break - } - } - break - } - } - - if ($billToName) { # Clean name from invalid filename characters $cleanName = ($billToName -replace '[\\/:*?"<>|]', '').Trim()