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
batchReplaceopens the folder by ID and asksDriveAppfor every file of type Google Docs. This skips PDFs, images, and anything else in the folder.- It loops through the Docs one at a time, opening each with
DocumentAppand grabbing its body. - For every find-and-replace pair in the
replacementsmap, it callsbody.replaceText, which swaps every occurrence in that Doc. - It calls
saveAndCloseon each Doc so the change is written and the Doc is released from memory before the next one opens. rebrandRunis the thin wrapper that supplies the real folder ID and the rebrand’s two replacement pairs, keepingbatchReplacereusable 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:
- Edit
rebrandRunso the folder ID and the find-and-replace pairs match your job. - In the Apps Script editor, select
rebrandRunand click Run. - Approve the authorisation prompt the first time — the script needs access to Drive and your Docs.
- Open a couple of Docs in the folder to confirm the change landed.
Watch out for
replaceTexttakes a regular expression, not a literal string. Characters like.()&and$have special meaning —northwind.comwill also matchnorthwindXcom. 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.
getFilesByTypedoes not recurse into subfolders. If your Docs are nested, walk subfolders too withgetFolders.- Replacements run in object order. If one pair’s output could be matched by a
later pair, the order of the
replacementsmap 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
Generate personalized study guides from notes
Reformat raw notes into structured study guides — for Northwind's internal training programme.
Updated Feb 8, 2026
Build a contract-clause assembly system
Construct Northwind agreements from a library of approved clauses — drag-drop in code.
Updated Feb 1, 2026
Translate and resolve Doc comments
Localise reviewer feedback on a shared Doc so multilingual teams can collaborate.
Updated Jan 25, 2026
Auto-archive finalized Docs to dated folders
File completed Northwind Docs by month so the active folder stays focused on in-flight work.
Updated Jan 18, 2026
Build a fillable intake form inside a Doc
Create structured intake forms with placeholder fields readers can fill — for client briefs.
Updated Jan 11, 2026