appscript.dev
Automation Beginner Drive

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

  1. renameInvoices is a thin wrapper that calls bulkRename with the configured folder and pattern. Add similar wrappers for other folders that need their own convention.
  2. bulkRename resolves the folder ID inside a try/catch, so a wrong ID logs a clear message instead of throwing.
  3. 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.
  4. It reads getLastUpdated() and formats it with DATE_FORMAT for the {date} placeholder, then expands all three placeholders into the new name.
  5. 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 nameNew name
invoice_final.pdf2025-03-04 invoice_final.pdf
Acme INV (2).pdf2025-03-11 Acme INV (2).pdf
scan0042.pdf2025-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:

  1. In the Apps Script editor, select renameInvoices and click Run.
  2. Approve the authorisation prompt the first time.
  3. 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 bulkRename for richer conventions.
  • {date} uses the file’s last-updated date, which is the last edit, not the original creation. Swap getLastUpdated() for getDateCreated() 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 setName call.

Related