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
setupYearAheadis a thin wrapper that callspreCreateArchiveFolderswith the configured root and horizon. Keeping the config in named constants means the trigger always runs with the same settings.preCreateArchiveFoldersresolves the parent folder inside atry/catch, so a wrong ID logs a clear message rather than throwing.- It loops
monthsAheadtimes. On each pass it copies today’s date and addsimonths withsetMonth, then formats the result asyyyy-MM. - 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. - 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:
- In the Apps Script editor, open Triggers (the clock icon).
- Click Add Trigger.
- Choose
setupYearAhead, event source Time-driven, type Month timer, and an early day of the month. - Save. Because the script skips folders that already exist, an extra run by hand never does any harm.
Watch out for
- The
yyyy-MMformat is what makes the folders sort correctly as text. ChangeFOLDER_FORMATto something likeMMM yyyyand 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 fresh2025-09is created alongside it. MONTHS_AHEADsets 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
Archive a project folder when it's marked done
Zip and shelve completed Northwind work — keep active folders focused on in-flight projects.
Updated Dec 11, 2025
Build a shared-drive migration helper
Move Northwind files between drives with structure intact — from My Drive to a Shared Drive.
Updated Oct 8, 2025
Build a version-snapshot system for key files
Keep dated copies of Northwind's critical Sheets before risky edits.
Updated Sep 22, 2025
Bulk-restore accidentally trashed files
Recover everything trashed in a date range — Northwind's emergency-undo button.
Updated Sep 14, 2025
Move stale files to cold storage
Archive Northwind Drive files untouched for a year into a `cold storage` folder.
Updated Aug 1, 2025