Get or make a folder by name
Apps Script recipes › Drive files
Finds a folder by name inside a parent (or makes it), so files land in Reports/2026/11 without duplicates.
File Code.gs
/** folderPath_(parent, ['Reports', '2026', '11']) → the last folder, made where missing. */
function folderPath_(parent, names) {
return names.reduce((folder, name) => {
const found = folder.getFoldersByName(name);
return found.hasNext() ? found.next() : folder.createFolder(name);
}, parent);
}
function monthFolder_() {
const tz = Session.getScriptTimeZone();
const now = new Date();
return folderPath_(DriveApp.getRootFolder(), ['Reports', Utilities.formatDate(now, tz, 'yyyy'), Utilities.formatDate(now, tz, 'MM')]);
}
Good to know
- Drive allows two folders with the same name; looking first keeps it to one.
Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.
Use it in a project