Guide
Batch rename files by invoice number
The specific job: a folder of invoices, and you want each file named after the invoice number printed on it.
Short answer
If the invoice numbers follow a consistent pattern and the PDFs have text, a short script does it free. The parts people get wrong are duplicates, illegal characters, and having no way back.
The free script version
For consistent documents this is about twenty lines and it will run for years.
- 1Install Python, then: pip install pymupdf
- 2For each PDF, open it and read the text of the first page.
- 3Find the invoice number with a regular expression that matches your suppliers' format, for example INV-\d+ or [A-Z]{2}\d{4,}.
- 4Build the new name, strip any characters Windows will not accept, and rename.
The four things that go wrong
- Two files with the same invoice number
- It happens more than you expect: a duplicate download, or an invoice and its matching receipt. If your script renames blindly, the second file silently replaces the first and you never find out. Always add a suffix instead.
- Characters Windows will not allow
- A filename cannot contain any of < > : " / \ | ? * and cannot end in a dot or a space. Dates are the usual culprit, because 27/08/2026 contains slashes. Replace them with hyphens rather than deleting them, or 27-08-2026 becomes 27082026 and stops being readable.
- A value that was not found
- If the invoice number is missing, do not build a name with a hole in it. A file called Thompson None.pdf is worse than one left alone. Skip it and put it on a list for a human.
- No way back
- Write down the old and new name of every file before you move anything, into a plain CSV next to the folder. Then a bad run is a five minute fix instead of an afternoon.
How to name them so they sort usefully
Filenames sort as text, not as dates, so put the parts in the order you want to browse in.
Supplier first groups everything from one company together. Date first, in year-month-day order, sorts the whole folder chronologically. Invoice number first is only useful if you look things up by number, which most people do not.
Year-month-day is the only date format that sorts correctly as text. 2026-08-27 sorts properly. 27-08-2026 does not.
Common questions
- Can I batch rename PDFs in Windows Explorer?
- You can rename many files at once, but only to a shared name with a number after it. Explorer cannot read what is inside a file, so it cannot use the invoice number.
- What if my invoice numbers have no consistent format?
- A regular expression will not work, because there is no pattern to match. You need something that understands what an invoice number is rather than what it looks like, or you go supplier by supplier with a rule for each.
- How do I undo a bulk rename?
- Only if you saved a record before you started. Write the old and new path of every file to a CSV first. Without it, there is nothing to reverse from.
Related
- Renaming PDFs by what is inside them
- Filing invoices into folders automatically
- Once, our free tool for this
Solven builds custom software and automation for Australian businesses. If you have a job like this that no tool quite fits, that is the work we do.
Get a free teardown