appscript.dev
Automation Beginner Drive

Pre-create dated archive folders

Generate per-month folders ahead of time so nothing lands in `misc/`.

Published Sep 2, 2025

Northwind files things by month — when the folder for that month exists. When it does not, the file gets dropped into misc/ “for now”, and misc/ is where files go to be forgotten. The friction is tiny but constant: nobody wants to stop and create a folder mid-task, so they don’t.

This script removes the friction by creating the next year of monthly folders in advance. Each folder is named yyyy-MM, so they sort correctly and there is always a right place to put a file. Run it on a monthly trigger and the archive structure stays a step ahead of the work.

What you’ll need

  • The ID of the parent folder that will hold the monthly folders — the string after /folders/ in its URL.
  • Edit access to that parent folder.
  • A decision on how far ahead to work — twelve months is a sensible default.

The script

// The parent folder that holds the dated monthly folders.
const ARCHIVE_ROOT_ID = '1abcArchiveRootId';

// How many months ahead to create folders for, including the current month.
const MONTHS_AHEAD = 12;

// Folder name format. yyyy-MM sorts correctly as plain text.
const FOLDER_FORMAT = 'yyyy-MM';

/**
 * Creates monthly archive folders for the next year inside the
 * configured root. Designed to run on a monthly trigger.
 */
function setupYearAhead() {
  preCreateArchiveFolders(ARCHIVE_ROOT_ID, MONTHS_AHEAD);
}

/**
 * Ensures a yyyy-MM folder exists for each of the next `monthsAhead`
 * months. Existing folders are left untouched, so this is safe to re-run.
 */
function preCreateArchiveFolders(parentId, monthsAhead) {
  // 1. Resolve the parent folder. Bail out early if the ID is wrong.
  let parent;
  try {
    parent = DriveApp.getFolderById(parentId);
  } catch (err) {
    Logger.log('Could not open the parent folder — check ARCHIVE_ROOT_ID.');
    return;
  }

  // 2. Step forward one month at a time from today.
  const today = new Date();
  let created = 0;
  for (let i = 0; i < monthsAhead; i++) {
    const month = new Date(today);
    month.setMonth(today.getMonth() + i);
    const name = Utilities.formatDate(month, 'GMT', FOLDER_FORMAT);

    // 3. Only create the folder if one with that name does not exist yet.
    if (!parent.getFoldersByName(name).hasNext()) {
      parent.createFolder(name);
      created++;
    }
  }

  Logger.log(`Pre-created ${created} new monthly folder(s) of ${monthsAhead} checked.`);
}

How it works

  1. setupYearAhead is a thin wrapper that calls preCreateArchiveFolders with the configured root and horizon. Keeping the config in named constants means the trigger always runs with the same settings.
  2. preCreateArchiveFolders resolves the parent folder inside a try/catch, so a wrong ID logs a clear message rather than throwing.
  3. It loops monthsAhead times. On each pass it copies today’s date and adds i months with setMonth, then formats the result as yyyy-MM.
  4. Before creating anything, it checks getFoldersByName(name) — if a folder with that name already exists, the month is skipped. That makes the script idempotent: re-running it never produces duplicates.
  5. It logs how many folders were newly created versus how many months it checked, so you can see at a glance whether the run did real work.

Example run

Run the script in September 2025 with MONTHS_AHEAD set to 12. The parent folder gains any of these that do not already exist:

Folders ensured
2025-09, 2025-10, 2025-11, 2025-12
2026-01, 2026-02, 2026-03, 2026-04
2026-05, 2026-06, 2026-07, 2026-08

A month later the trigger runs again: eleven of those already exist, so only 2026-09 is new. The log reads Pre-created 1 new monthly folder(s) of 12 checked.

Trigger it

Run this monthly so the folder structure always stays a year ahead:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose setupYearAhead, event source Time-driven, type Month timer, and an early day of the month.
  4. Save. Because the script skips folders that already exist, an extra run by hand never does any harm.

Watch out for

  • The yyyy-MM format is what makes the folders sort correctly as text. Change FOLDER_FORMAT to something like MMM yyyy and the folders will sort alphabetically — April before January — which defeats the point.
  • It only creates folders; it never moves files into them. Pair this with a separate sorting step if you also want files filed automatically.
  • The duplicate check matches on exact name. A folder called 2025-09 (old) will not be treated as the September folder, so a fresh 2025-09 is created alongside it.
  • MONTHS_AHEAD sets the horizon, not the past. The script never creates folders for months before the current one — back-fill those by hand if you need them.

Related