Google App Script Snippet: Bulk rename Google Drive files

Photo by Arturo Castaneyra on Unsplash

I ran into an issue yesterday, where I downloaded a bunch of files with another script into my Google Drive. The problem was that the API Key was still added as a query string after the file extension. So none of the files would actually open correctly, due the faulty extension.

I’ve found a few add-ons and Chrome extensions that did a bulk rename, but it wouldn’t have solved my problem.

So I’ve written a little script that renames the files. The cool thing with the script is that you can do some crazy manipulation, because its just using plain old JavaScript.

function renameFiles() {
var folders = DriveApp.getFoldersByName('[YOUR FOLDER NAME]');
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
// This is specific for our scenario.
// We wanted to remove everything after the extension. 'The ?key=' was after our extension.
if (fileName.indexOf('?key=') > -1) {
fileName= fileName.split('?key=')[0];
file.setName(fileName);
}
}
}
}

Remember to replace [YOUR FOLDER NAME] with your folder name.

Give me a clap if this helped you.

--

--