Apps Script example · 5 min read

Build a Year / Month / Project Folder Structure in Drive: Copy-Paste Apps Script Pattern

Working build a year / month / project folder structure in drive example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

DriveFoldersAutomation

Manually creating a new folder every time a project starts is the kind of chore that gets skipped under deadline pressure, which is exactly when a consistent Drive structure matters most.

This script takes a project name and builds out a Year, then Month, then Project folder underneath a fixed root, reusing any level that already exists instead of creating duplicates.

Because the function checks for existing folders at each level before creating a new one, you can run it repeatedly for different projects in the same month without ending up with several folders that have the same name.

Once wired up, this becomes the entry point other scripts in this series can call whenever they need a destination folder for a new project's files.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

ResourceName / valuePurpose
Root folderProjectsParent Drive folder ID in CONFIG
Child patternYear / Month / ProjectNameNested folders created by script
InputProject name stringPassed into createProjectFolders()

What It Does

Given a root folder and a project name, the script walks three levels deep, creating a folder for the current year, then a folder for the current month inside that, then a folder for the project inside the month.

At each level it first checks whether a folder with that name already exists using getFoldersByName, and only creates a new one when nothing is found, which keeps repeated calls idempotent.

Prerequisites

You need a root Projects folder in Drive with its ID copied from the folder's URL, since every year folder is created as a direct child of that fixed root.

Decide on a naming convention for project folders ahead of time, such as removing punctuation, so folders created by different people or scripts stay consistent.

Walkthrough

Paste the createProjectFolder function into the script editor, set ROOT_FOLDER_ID to your Projects folder's ID, and call the function with a project name as its argument.

Run createProjectFolder('Website Redesign') from the editor and confirm in Drive that a folder chain like 2026, 07, Website Redesign appeared under the root.

Run the same call a second time and verify no duplicate folders were created at any level, confirming the existence checks are working before you wire this into other automations.

Edge Cases

Project names containing slashes or other characters that look like path separators are passed through as-is, since Drive folder names allow most punctuation; sanitize upstream if that matters for your workflow.

If two people run the script for the same project in the same month at nearly the same moment, there is a small chance of a race condition creating two folders with the same name, since getFoldersByName and createFolder are not atomic together.

Testing

Call the function with three different project names in the same month and confirm all three project folders sit correctly under the same year and month folders rather than creating separate month folders.

Change your script's time zone setting temporarily and confirm the month folder name still matches the calendar month you expect, since Utilities.formatDate depends on Session.getScriptTimeZone.

Hardening

Add a getFoldersByName check for the exact project name before creating the final folder so accidentally calling the function twice for the same project reuses the existing folder instead of adding a numbered duplicate.

Log the full folder path and its Drive ID to a tracking sheet each time a project folder is created, so other scripts and teammates can look up the destination without digging through Drive manually.

Variations

Swap the Year/Month split for a Quarter-based structure by computing the quarter number from the current month before building the folder chain.

Trigger folder creation directly from a form submission so filling out a New Project form automatically produces the matching Drive folder without anyone touching the script editor.

createProjectFolder.gs

createProjectFolder and its getOrCreateFolder helper build a Year, Month, and Project folder chain under a fixed root, reusing any level that already exists.

// Create nested Year/Month/Project folders in Drive
function createProjectFolder(projectName) {
  var ROOT_FOLDER_ID = 'REPLACE_WITH_ROOT_ID';
  var root = DriveApp.getFolderById(ROOT_FOLDER_ID);
  var now = new Date();
  var tz = Session.getScriptTimeZone();
  var year = Utilities.formatDate(now, tz, 'yyyy');
  var month = Utilities.formatDate(now, tz, 'MM');

  var yearFolder = getOrCreateFolder(root, year);
  var monthFolder = getOrCreateFolder(yearFolder, month);
  var projectFolder = getOrCreateFolder(monthFolder, projectName);

  Logger.log('Project folder ready: ' + projectFolder.getUrl());
  return projectFolder;
}

function getOrCreateFolder(parent, name) {
  var existing = parent.getFoldersByName(name);
  if (existing.hasNext()) {
    return existing.next();
  }
  return parent.createFolder(name);
}
  1. Line 10: Building the year folder first means every later level nests correctly underneath it rather than being created at the root.
  2. Line 12: The project folder is the final, most specific level, created directly inside that month's folder.
  3. Line 18: Factoring the existence check into its own helper means the same reuse-or-create logic works identically at all three levels.
  4. Line 20: Checking hasNext before creating anything is what makes repeated calls for the same project safe instead of piling up duplicates.
  5. Line 23: createFolder only runs when nothing matching was found, so this line is the only place a brand-new folder actually gets made.

Deploy this example

  1. 01

    Open Apps Script

    In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.

  2. 02

    Paste and save

    Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.

  3. 03

    Authorize once

    Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.

  4. 04

    Add the trigger

    Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.

Before you run: create folder structure

  • 1Root 'Projects' folder created in Drive with its ID copied
  • 2Project naming convention agreed on with the team
  • 3getOrCreateFolder helper pasted alongside the main function
  • 4Function tested with a sample project name
  • 5Repeat run confirmed not to create duplicate folders
  • 6Time zone setting checked against expected month naming

Frequently asked questions

The existence check at each folder level means the second call simply returns the same folder rather than creating a duplicate.

Yes, compute the quarter number from the current month and call getOrCreateFolder with that value instead of the two-digit month string.

There is a small race-condition risk noted in the edge-cases section; for high-concurrency use, consider adding a LockService lock around folder creation.

This script only builds the folder hierarchy; combine it with the move-files-by-name tutorial to actually populate the new folders.

Yes, call createProjectFolder from inside an onFormSubmit trigger function using the submitted project name as the argument.

It returns the final DriveApp Folder object for the project level, which you can use immediately to create files inside it.

Related examples

Want this wired into your real workflow?

I adapt these patterns to your Sheet structure, APIs, and triggers — deployed in your Google account. Fixed-scope quotes from $500 · free 30-min consult · quote within 24 hours.