Batch Rename Files in Google Drive Using Apps Script

Renaming dozens of files in Google Drive by hand is a chore. Apps Script can do it in seconds — whether you need to add a date prefix, replace text in filenames, or apply a numbered sequence.

Add a prefix to all files in a folder

function addPrefixToFiles() { const folderId = 'YOUR_FOLDER_ID'; const prefix = '2025_'; const folder = DriveApp.getFolderById(folderId); const files = folder.getFiles(); let count = 0; while (files.hasNext()) { const file = files.next(); const oldName = file.getName(); file.setName(prefix + oldName); Logger.log(`Renamed: "${oldName}" → "${prefix + oldName}"`); count++; } Logger.log(`Done. Renamed ${count} file(s).`); }

Add a suffix (before the file extension)

function addSuffixToFiles() { const folderId = 'YOUR_FOLDER_ID'; const suffix = '_final'; const folder = DriveApp.getFolderById(folderId); const files = folder.getFiles(); while (files.hasNext()) { const file = files.next(); const name = file.getName(); const dotIndex = name.lastIndexOf('.'); let newName; if (dotIndex !== -1) { newName = name.slice(0, dotIndex) + suffix + name.slice(dotIndex); } else { newName = name + suffix; } file.setName(newName); Logger.log(`Renamed: "${name}" → "${newName}"`); } }

Find and replace text in filenames

function findAndReplaceInNames() { const folderId = 'YOUR_FOLDER_ID'; const findText = 'Draft'; const replaceText = 'Final'; const folder = DriveApp.getFolderById(folderId); const files = folder.getFiles(); while (files.hasNext()) { const file = files.next(); const oldName = file.getName(); if (oldName.includes(findText)) { const newName = oldName.replaceAll(findText, replaceText); file.setName(newName); Logger.log(`Renamed: "${oldName}" → "${newName}"`); } } }

Apply a numbered sequence

function renameWithSequence() { const folderId = 'YOUR_FOLDER_ID'; const baseName = 'Report'; const padLength = 3; // e.g., Report_001, Report_002 const folder = DriveApp.getFolderById(folderId); const files = folder.getFiles(); const allFiles = []; while (files.hasNext()) { allFiles.push(files.next()); } // Sort by creation date allFiles.sort((a, b) => a.getDateCreated() - b.getDateCreated()); allFiles.forEach((file, i) => { const num = String(i + 1).padStart(padLength, '0'); const ext = file.getName().includes('.') ? '.' + file.getName().split('.').pop() : ''; const newName = `${baseName}_${num}${ext}`; file.setName(newName); Logger.log(`Renamed: "${file.getName()}" → "${newName}"`); }); }

Rename using data from a Google Sheet

For full control, list the old and new names in a Sheet:

A: File IDB: New Name
1abc...Q1 Report
2def...Q2 Report
function renameFromSheet() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const data = sheet.getDataRange().getValues().slice(1); // Skip header data.forEach(([fileId, newName]) => { if (!fileId || !newName) return; try { const file = DriveApp.getFileById(fileId); const oldName = file.getName(); file.setName(newName); Logger.log(`Renamed: "${oldName}" → "${newName}"`); } catch (e) { Logger.log(`Error renaming file ${fileId}: ${e.message}`); } }); }

Tips

  • To get a file's ID, right-click it in Google Drive > Get link — the ID is the string after /d/ in the URL.
  • folder.getFiles() only returns direct children, not files in subfolders. Use recursion if you need to rename files in nested folders.
  • Always log old and new names before running on large batches — it's easy to spot a pattern issue before it affects hundreds of files.
  • There's no undo in Drive for renames. Test on a small folder copy first.