GAS AI BuilderApps Script Assistant Sign in

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

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

More in Drive files

Share a file with someoneGives one person view or edit access to a file without the notification email Google sends by default.Read the text of a PDF or photo (OCR)Turns a PDF or picture into text with Google's OCR through the Advanced Drive service (v2 or v3), then removes the temporary Doc.