Update RenameInvoice.ps1
Attempt to refine the regex search of `Bill to` so it doesn't use other invoice parts for the name
This commit is contained in:
parent
e085d9dadc
commit
9345f0e986
@ -1,4 +1,4 @@
|
|||||||
# Ensure 'pdftotext.exe' is available in your PATH or specify full path here
|
# Ensure 'pdftotext.exe' is available in your PATH or specify the full path here
|
||||||
$pdftotextPath = "pdftotext.exe"
|
$pdftotextPath = "pdftotext.exe"
|
||||||
|
|
||||||
# Get all PDF files in the script's current directory
|
# Get all PDF files in the script's current directory
|
||||||
@ -11,41 +11,53 @@ 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
|
# Read extracted content line-by-line
|
||||||
$content = Get-Content $tempTxt
|
$lines = Get-Content $tempTxt
|
||||||
|
|
||||||
# Initialize variables
|
# Initialize variable to store Bill To name
|
||||||
$billToName = $null
|
$billToName = $null
|
||||||
$foundBillTo = $false
|
|
||||||
|
|
||||||
# Search line-by-line for the correct 'Bill to' line
|
# Find the line with 'Bill to' exactly and take the next non-empty line
|
||||||
for ($i = 0; $i -lt $content.Length; $i++) {
|
for ($i = 0; $i -lt $lines.Length; $i++) {
|
||||||
if ($content[$i] -match "Bill to\s*$") {
|
if ($lines[$i].Trim() -eq "Bill to") {
|
||||||
# Find the next non-empty line after "Bill to"
|
# Find the next non-empty line as the company name
|
||||||
for ($j = $i + 1; $j -lt $content.Length; $j++) {
|
for ($j = $i + 1; $j -lt $lines.Length; $j++) {
|
||||||
if ($content[$j].Trim()) {
|
$nextLine = $lines[$j].Trim()
|
||||||
$billToName = $content[$j].Trim()
|
if ($nextLine -ne "") {
|
||||||
|
$billToName = $nextLine
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($billToName) { break }
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($billToName) {
|
if ($billToName) {
|
||||||
# Generate new filename
|
# Clean up billToName (remove invalid filename chars)
|
||||||
$newFileName = "$billToName 2025 calendar ad invoice.pdf"
|
$cleanName = ($billToName -replace '[\\/:*?"<>|]', '').Trim()
|
||||||
|
|
||||||
# Check if the target file already exists
|
# Generate new filename safely
|
||||||
if (-not (Test-Path "$($pdfFile.DirectoryName)\$newFileName")) {
|
$newFileName = "$cleanName 2025 calendar ad invoice.pdf"
|
||||||
# Rename file
|
|
||||||
Rename-Item -Path $pdfFile.FullName -NewName $newFileName -Verbose
|
# Ensure the filename isn't excessively long
|
||||||
|
if ($newFileName.Length -gt 200) {
|
||||||
|
$newFileName = $newFileName.Substring(0, 200) + ".pdf"
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
# Build full destination path explicitly
|
||||||
|
$destPath = Join-Path -Path $pdfFile.DirectoryName -ChildPath $newFileName
|
||||||
|
|
||||||
|
# Check if the file already exists
|
||||||
|
if (-not (Test-Path $destPath)) {
|
||||||
|
try {
|
||||||
|
Rename-Item -Path $pdfFile.FullName -NewName $newFileName -Verbose -ErrorAction Stop
|
||||||
|
} catch {
|
||||||
|
Write-Warning "Error renaming '$($pdfFile.Name)': $_"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
Write-Warning "File '$newFileName' already exists. Skipping rename."
|
Write-Warning "File '$newFileName' already exists. Skipping rename."
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Write-Warning "Could not find 'Bill to' in file '$($pdfFile.Name)'. Skipping file."
|
Write-Warning "Could not find 'Bill to' in file '$($pdfFile.Name)'. Skipping file."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user