'Bulk rename file names in Drive Folder with Google Sheets
I need to rename image files in a Google Drive folder
I have a Google Sheet with 2 columns, old name and new name
I would like to rename the files within the same folder using AppsScript and the sheet content as input
Thanks for your help
Solution 1:[1]
Issue:
If I understand this correctly, you want change names of the files in a Drive folder according to some sheet data (current name is in column A, and new name is in column B).
Solution:
If that's the case, I'd suggest the following:
- Get all sheet values with
getRange().getValues(). - Iterate through all value rows.
- For each row, find the folder files whose name is the value from column A, using Folder.getFilesByName.
- Set the new file name with File.setName().
Code snippet:
const SPREADSHEET_ID = "SPREADSHEET_ID"; // Change according to your preferences
const SHEET_NAME = "Sheet1"; // Change according to your preferences
const FOLDER_ID = "DRIVE_FOLDER_ID"; // Change according to your preferences
function changeFolderNames() {
const ss = SpreadsheetApp.openById(SPREADSHEET_ID);
const sheet = ss.getSheetByName(SHEET_NAME);
const folder = DriveApp.getFolderById(FOLDER_ID);
const values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues(); // If there's no header row, use "A:B" instead
values.forEach(row => {
const [oldName, newName] = row;
const fileIter = folder.getFilesByName(oldName);
while (fileIter.hasNext()) {
const file = fileIter.next();
file.setName(newName);
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Iamblichus |
