A sheet with a file id column and a new-name column, and a pile of Drive files that still say IMG_4021 or Copy of Proposal. renameFilesFromRows(rows, { dryRun }) walks { fileId, newName } rows, renames each file (or only logs on a dry run), and returns a status object per row ready to write back.
When to use
- Bulk renames from a sheet list after an export or a form dump
- A dry-run pass before anything in Drive actually changes
- Writing
renamed/error/would-renameback into a Status column
How to use this snippet
/**
* Rename Drive files from {fileId, newName} rows.
* With dryRun:true, reports what would change and writes nothing.
*
* @param {{fileId: string, newName: string}[]} rows
* @param {{dryRun?: boolean}} [opts]
* @return {{fileId: string, newName: string, oldName?: string, status: string, detail: string}[]}
*/
function renameFilesFromRows(rows, opts) {
opts = opts || {};
var dryRun = !!opts.dryRun;
return (rows || []).map(function (row) {
var fileId = row && row.fileId != null ? String(row.fileId).trim() : '';
var newName = row && row.newName != null ? String(row.newName).trim() : '';
if (!fileId) return { fileId: fileId, newName: newName, status: 'skipped', detail: 'missing fileId' };
if (!newName) return { fileId: fileId, newName: newName, status: 'skipped', detail: 'missing newName' };
try {
var file = DriveApp.getFileById(fileId);
var oldName = file.getName();
if (dryRun) {
return { fileId: fileId, newName: newName, oldName: oldName, status: 'would-rename', detail: oldName + ' → ' + newName };
}
if (oldName === newName) {
return { fileId: fileId, newName: newName, oldName: oldName, status: 'unchanged', detail: 'already named' };
}
file.setName(newName);
return { fileId: fileId, newName: newName, oldName: oldName, status: 'renamed', detail: oldName + ' → ' + newName };
} catch (e) {
return { fileId: fileId, newName: newName, status: 'error', detail: String(e && e.message ? e.message : e) };
}
});
}
Example
function renameFromSheet(dryRun) {
var sh = SpreadsheetApp.getActive().getSheetByName('Rename');
var data = sh.getDataRange().getValues();
var headers = data[0];
var idCol = headers.indexOf('File Id');
var nameCol = headers.indexOf('New Name');
var rows = data.slice(1).map(function (r) {
return { fileId: r[idCol], newName: r[nameCol] };
});
var results = renameFilesFromRows(rows, { dryRun: dryRun });
var statusCol = headers.indexOf('Status') + 1;
results.forEach(function (r, i) {
sh.getRange(i + 2, statusCol).setValue(r.status + ': ' + r.detail);
});
}
Tips:
- Statuses:
renamed,unchanged(already that name),would-rename(dry run),skipped(blank id or name),error(missing file, no permission). - Dry run never calls
setName. Run it first, scan the Status column, then call again withdryRun: false. - Normalize pasted Share links with
extractDriveFileIdbefore building the rows. - Drive renames are per-file; a shared file's name changes for everyone who can see it.
Tip: NitroGAS Co-Pilot can add the dry-run menu item and the Status write-back. Paste your header row.
Happy Coding!
