Update RenameInvoice.ps1
Updated the script to perform a full-text regex match
This commit is contained in:
parent
a633cc1f97
commit
779a37cfa8
@ -11,27 +11,13 @@ foreach ($pdfFile in $pdfFiles) {
|
|||||||
# Extract text content from PDF
|
# Extract text content from PDF
|
||||||
& $pdftotextPath -layout $pdfFile.FullName $tempTxt
|
& $pdftotextPath -layout $pdfFile.FullName $tempTxt
|
||||||
|
|
||||||
# Read extracted content line-by-line
|
# Read extracted content as a single string
|
||||||
$lines = Get-Content $tempTxt
|
$content = Get-Content $tempTxt -Raw
|
||||||
|
|
||||||
# Initialize Bill To name
|
# More robust method: find "Bill to" and capture next meaningful line
|
||||||
$billToName = $null
|
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
|
# Clean name from invalid filename characters
|
||||||
$cleanName = ($billToName -replace '[\\/:*?"<>|]', '').Trim()
|
$cleanName = ($billToName -replace '[\\/:*?"<>|]', '').Trim()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user