Get or Create a Drive Folder by Name

Exports, backups, and client drop folders all need a stable place in Drive. This returns the first child folder with that name under a parent, or creates it.

What you'll need

  • A parent Folder (DriveApp.getRootFolder(), or a known folder ID)
  • Drive create/read permission for that parent

How to use this snippet

function getOrCreateFolder(parent, name) { var folders = parent.getFoldersByName(name); if (folders.hasNext()) { return folders.next(); } return parent.createFolder(name); } function ensureExportFolder() { var root = DriveApp.getFolderById('YOUR_PARENT_FOLDER_ID'); var year = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy'); return getOrCreateFolder(root, 'Exports-' + year); }

Tips:

  • If multiple folders share the name, this returns the first — keep names unique under a parent.
  • Prefer folder IDs in production scripts so renames don't break paths.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension