appscript.dev
Automation Beginner Docs Drive

Batch-replace placeholders across many Docs

Run find-and-replace over an entire Drive folder of Docs in one pass.

Published Jul 6, 2025

Northwind has rebranded, and the change has to reach every Doc in the branding/ folder — Northwind & Partners swapped for Northwind Studios, the old domain swapped for the new one. Opening dozens of Docs and running find-and-replace by hand is exactly the kind of slow, error-prone job that gets half-done and forgotten.

This script does the whole folder in one pass. Give it a folder and a set of find-and-replace pairs, and it walks every Google Doc inside, applies each replacement, and saves. One run, every Doc consistent.

What you’ll need

  • A Drive folder containing the Google Docs you want to update. Note its ID — it is the part of the folder URL after /folders/.
  • The exact text to find and the text to replace it with, for each substitution you need to make.
  • Edit access to every Doc in the folder, so the script can save its changes.

The script

/**
 * Runs a set of find-and-replace pairs over every Google Doc in a folder.
 *
 * @param {string} folderId      ID of the Drive folder to process.
 * @param {Object} replacements  Map of find-text to replace-text.
 */
function batchReplace(folderId, replacements) {
  // 1. Get every Google Doc in the folder (subfolders are not included).
  const files = DriveApp.getFolderById(folderId)
    .getFilesByType(MimeType.GOOGLE_DOCS);

  let count = 0;

  // 2. Walk the folder one Doc at a time.
  while (files.hasNext()) {
    const file = files.next();
    const doc = DocumentApp.openById(file.getId());
    const body = doc.getBody();

    // 3. Apply every find-and-replace pair to this Doc's body.
    for (const [from, to] of Object.entries(replacements)) {
      body.replaceText(from, to);
    }

    // 4. Save and close so the changes persist and memory is freed.
    doc.saveAndClose();
    count++;
  }

  Logger.log('Updated ' + count + ' Docs.');
}

/**
 * Convenience wrapper for the Northwind rebrand — the folder ID and the
 * replacement pairs live here so batchReplace stays generic.
 */
function rebrandRun() {
  batchReplace('1abcBrandingFolderId', {
    'Northwind & Partners': 'Northwind Studios',
    'northwind.com': 'northwind.studio',
  });
}

How it works

  1. batchReplace opens the folder by ID and asks DriveApp for every file of type Google Docs. This skips PDFs, images, and anything else in the folder.
  2. It loops through the Docs one at a time, opening each with DocumentApp and grabbing its body.
  3. For every find-and-replace pair in the replacements map, it calls body.replaceText, which swaps every occurrence in that Doc.
  4. It calls saveAndClose on each Doc so the change is written and the Doc is released from memory before the next one opens.
  5. rebrandRun is the thin wrapper that supplies the real folder ID and the rebrand’s two replacement pairs, keeping batchReplace reusable for any future find-and-replace job.

Example run

The branding/ folder holds three Docs. Before the run, one of them contains:

Prepared by Northwind & Partners. Visit northwind.com for details.

After rebrandRun, the same line in all three Docs reads:

Prepared by Northwind Studios. Visit northwind.studio for details.

The execution log confirms the scope: Updated 3 Docs.

Run it

This is an on-demand job, so run it by hand when a rebrand or correction is needed:

  1. Edit rebrandRun so the folder ID and the find-and-replace pairs match your job.
  2. In the Apps Script editor, select rebrandRun and click Run.
  3. Approve the authorisation prompt the first time — the script needs access to Drive and your Docs.
  4. Open a couple of Docs in the folder to confirm the change landed.

Watch out for

  • replaceText takes a regular expression, not a literal string. Characters like . ( ) & and $ have special meaning — northwind.com will also match northwindXcom. Escape them (northwind\\.com) when you need an exact match.
  • The replacement is permanent and there is no undo across files. Test on a copy of the folder first, or keep the originals backed up.
  • getFilesByType does not recurse into subfolders. If your Docs are nested, walk subfolders too with getFolders.
  • Replacements run in object order. If one pair’s output could be matched by a later pair, the order of the replacements map matters.
  • A folder with hundreds of large Docs can approach the 6-minute execution limit. For very large folders, process in batches or split across triggers.

Related