Skip to content
// Drive · Apps Script

Rename multiple files at once in Google Drive.

Use Google Apps Script to batch-rename files in Drive with a pattern replace. searchFiles uses Drive query syntax to match by title substring; the actual string replacement happens in JavaScript after you have the file handles.

I need to rename a large batch of Google Drive files that share a naming pattern, without clicking through each one manually.

The script

copy · paste · trigger
renameFiles.gs
Apps Script
// Rename Drive files: replace oldPart with newPart in every matching title
function renameMatchingFiles() {
  var oldPart = 'Report_2023';
  var newPart = 'Report_2024';
  var query = 'title contains "' + oldPart + '" and trashed = false';

  var files = DriveApp.searchFiles(query);
  var count = 0;

  while (files.hasNext()) {
    var file = files.next();
    var oldName = file.getName();
    var newName = oldName.split(oldPart).join(newPart);
    if (newName !== oldName) {
      file.setName(newName);
      count++;
    }
  }

  Logger.log('Renamed ' + count + ' file(s).');
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

What searchFiles can and cannot do

The Drive query language accepts a small fixed set of operators: title contains, title = , fullText contains, mimeType =, and a handful of others. It does not accept regular expressions. That means you cannot write a query like 'title matches /Report_20\d{2}/' and expect Drive to filter for you.

The practical split: use the Drive query to narrow the result set to files whose title contains the substring you want to replace, then do the actual pattern work in JavaScript once you have the file objects. The script above uses split().join() rather than replace() because replace() without a regex only swaps the first occurrence; split().join() replaces all occurrences in one step, which matters when your substring appears more than once in a filename.

The trashed = false clause is worth keeping. Without it, searchFiles returns files sitting in the trash, and renaming them is legal but confusing when they resurface after a restore.

Scope, folder limits, and the 200-file ceiling

DriveApp.searchFiles searches your entire Drive, including files shared with you where you have edit access. If you only want to touch one folder, swap to folder.searchFiles(query) after grabbing the folder with DriveApp.getFolderById('FOLDER_ID'). The folder ID is the string at the end of the folder URL.

Apps Script's Drive service returns results in pages of up to 200 files per internal call, but the FileIterator returned by searchFiles handles pagination transparently. The while (files.hasNext()) loop will walk through all pages automatically, so you do not need to manage page tokens.

One real limit: if your rename operation touches several hundred files in a single execution, you may hit Apps Script's 6-minute execution time limit. The first time I hit this I added a counter and broke the work into two runs by adding a title contains filter to skip files already renamed. A simple workaround for most batches under a few thousand files.

Running it and confirming the output

Open the script from script.google.com or from Extensions > Apps Script inside any Google Sheet. Paste the function, set oldPart and newPart to match your actual filenames, then click Run.

The first run will prompt for Drive access authorization. After granting it, run again. Check the Execution Log (View > Logs or Ctrl+Enter) for the 'Renamed N file(s).' line. If count is 0, your query string did not match any titles, which usually means the substring you put in oldPart does not appear literally in the filenames, or the files are in Shared Drives, which require DriveApp.getSharedDriveFiles() with supportsAllDrives enabled.

Shared Drives are the one common gotcha here: the standard DriveApp.searchFiles does not reach them by default. If your files live in a Shared Drive, you need the Drive advanced service (Drive.Files.list with includeItemsFromAllDrives: true and supportsAllDrives: true), not the built-in DriveApp class.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Can I use a regex to find files in the Drive query string?
No. Drive query syntax only supports title contains 'substring' and title = 'exact'. Do the regex match in JavaScript after retrieving the files with searchFiles.
How do I limit the rename to one specific folder instead of all of Drive?
Replace DriveApp.searchFiles(query) with DriveApp.getFolderById('YOUR_FOLDER_ID').searchFiles(query). The folder ID is the last segment of the folder's URL in Drive.
Why does my count show 0 even though the files exist?
Usually the substring in your query does not match the literal filename. Drive search is case-sensitive for title contains. Check for extra spaces, different capitalization, or files that live in a Shared Drive (which searchFiles skips by default).
What happens if the same substring appears twice in one filename?
split(oldPart).join(newPart) replaces every occurrence. String.replace(oldPart, newPart) without a regex flag only replaces the first one, so the split/join pattern is safer for filenames that repeat the target string.
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.