Bulk-rename files to a naming convention
Apply consistent names across a Northwind folder — e.g., `YYYY-MM-DD ClientName Invoice.pdf`.
Published Jul 20, 2025
Northwind’s invoices folder is a museum of naming habits: invoice_final.pdf,
Acme INV (2).pdf, scan0042.pdf. Every file is fine on its own, but as a set
they refuse to sort, refuse to search, and make it impossible to glance at the
folder and know what is there. The fix is a single convention, applied to
everything at once.
This script renames every file in a folder to a pattern you define, built from
placeholders like {date} and {original}. Point it at a folder, pick a
pattern, and the whole folder snaps into a consistent, sortable shape. It is a
one-off cleanup, not a scheduled job — run it, check the result, move on.
What you’ll need
- The ID of the folder whose files you want to rename — the string after
/folders/in its URL. - A naming pattern made from the supported placeholders:
{date}(the file’s last-updated date),{original}(the old name without its extension), and{ext}(the extension, dot included). - Edit access to the folder. Renaming is permanent, so snapshot the folder first if you are unsure — see “Watch out for”.
The script
// The folder whose files will be renamed.
const RENAME_FOLDER_ID = '1abcInvoicesId';
// The naming pattern. Supported placeholders: {date}, {original}, {ext}.
const NAME_PATTERN = '{date} {original}{ext}';
// Date format used for the {date} placeholder.
const DATE_FORMAT = 'yyyy-MM-dd';
/**
* Renames every file in a folder to a pattern built from placeholders.
* Reads RENAME_FOLDER_ID and NAME_PATTERN from the config above.
*/
function renameInvoices() {
bulkRename(RENAME_FOLDER_ID, NAME_PATTERN);
}
/**
* Renames every file in the given folder by expanding the pattern's
* placeholders against each file's own name and last-updated date.
*/
function bulkRename(folderId, pattern) {
// 1. Resolve the folder. Bail out early if the ID is wrong.
let folder;
try {
folder = DriveApp.getFolderById(folderId);
} catch (err) {
Logger.log('Could not open the folder — check the folder ID.');
return;
}
// 2. Walk the folder and rename each file in turn.
const files = folder.getFiles();
let renamed = 0;
while (files.hasNext()) {
const file = files.next();
const oldName = file.getName();
// 3. Pull the extension (with its dot) and the base name without it.
const ext = oldName.match(/\.[^.]+$/)?.[0] || '';
const base = oldName.replace(/\.[^.]+$/, '');
const date = Utilities.formatDate(file.getLastUpdated(), 'GMT', DATE_FORMAT);
// 4. Expand the placeholders into the final name.
const newName = pattern
.replace('{date}', date)
.replace('{original}', base)
.replace('{ext}', ext);
// 5. Skip files that already match — avoids needless rename churn.
if (newName === oldName) continue;
file.setName(newName);
renamed++;
}
Logger.log(`Renamed ${renamed} file(s) in "${folder.getName()}".`);
}
How it works
renameInvoicesis a thin wrapper that callsbulkRenamewith the configured folder and pattern. Add similar wrappers for other folders that need their own convention.bulkRenameresolves the folder ID inside atry/catch, so a wrong ID logs a clear message instead of throwing.- For each file it splits the name into a base and an extension using a
regular expression.
{ext}keeps the leading dot;{original}is the base with no extension, so you control exactly where the dot lands. - It reads
getLastUpdated()and formats it withDATE_FORMATfor the{date}placeholder, then expands all three placeholders into the new name. - If the new name already equals the old one, the file is skipped — re-running the script is therefore safe and only touches files that still need it.
Example run
With NAME_PATTERN set to {date} {original}{ext}, a folder of mismatched
invoices is rewritten like this:
| Old name | New name |
|---|---|
invoice_final.pdf | 2025-03-04 invoice_final.pdf |
Acme INV (2).pdf | 2025-03-11 Acme INV (2).pdf |
scan0042.pdf | 2025-03-18 scan0042.pdf |
Every file now leads with an ISO date, so the folder sorts chronologically by
name alone. Switch the pattern to {original} — archived{ext} and the same
files would instead gain a consistent suffix.
Run it
This is a one-off cleanup, so run it by hand and check the result:
- In the Apps Script editor, select
renameInvoicesand click Run. - Approve the authorisation prompt the first time.
- Open the folder in Drive and confirm the names look right. The execution log reports how many files were renamed.
Watch out for
- Renaming is permanent and there is no undo. Before the first run, take a copy of the folder, or see Build a version-snapshot system for key files for a repeatable safety net.
- The pattern only knows three placeholders. Anything like a client name has to
come from somewhere — the original name, a sheet lookup — so you may need to
extend
bulkRenamefor richer conventions. {date}uses the file’s last-updated date, which is the last edit, not the original creation. SwapgetLastUpdated()forgetDateCreated()if you need the date the file first appeared.- It renames every file in the folder indiscriminately. If the folder mixes
invoices with unrelated files, move the strays out first or add a name filter
before the
setNamecall.
Related
Build a recurring file-delivery system
Drop a fresh report file into a Northwind client folder weekly — they don't even ask.
Updated Dec 15, 2025
Build a Drive search index in Sheets
Make Northwind's file metadata searchable in a Sheet — like Spotlight for Drive.
Updated Dec 7, 2025
Build a shared-folder onboarding kit
Auto-grant new Northwind hires the folders they need on day one.
Updated Nov 29, 2025
Route saved email attachments to project folders
File Gmail attachments into the right Northwind client folder based on subject keywords.
Updated Nov 25, 2025
Bundle a folder of images into one PDF
Combine Northwind scans into a single deliverable PDF using a generation service.
Updated Nov 17, 2025