appscript.dev
Automation Beginner Docs Drive

Auto-archive finalized Docs to dated folders

File completed Northwind Docs by month so the active folder stays focused on in-flight work.

Published Jan 18, 2026

Northwind keeps in-progress Docs in a single active folder, and it slowly turns into a junk drawer. Finished work sits next to live drafts, and finding the thing you are actually working on means scrolling past months of completed proposals and reports.

This script keeps the active folder lean. On a schedule it scans for any Doc whose name is tagged [final], then moves it into a dated archive folder for the current month — creating that folder if it does not exist yet. Finished work is filed away by month; the active folder shows only what is still in progress.

What you’ll need

  • An active Drive folder where in-progress Docs live.
  • An archive root Drive folder where dated sub-folders will be created.
  • A naming convention: when a Doc is done, add [final] somewhere in its name (the match is case-insensitive). That tag is the only signal the script uses.
  • The IDs of both folders, taken from their URLs.

The script

// Folder where in-progress Docs live and get scanned.
const ACTIVE = '1abcActiveDocsFolderId';

// Parent folder under which monthly archive sub-folders are created.
const ARCHIVE_ROOT = '1abcArchiveRootId';

// A Doc is archived when its name contains this tag (case-insensitive).
const FINAL_TAG = '[final]';

/**
 * Moves every finalised Doc out of the active folder and into a
 * year-month sub-folder under the archive root.
 */
function archiveFinalisedDocs() {
  const files = DriveApp.getFolderById(ACTIVE).getFiles();
  let moved = 0;

  // Walk every file in the active folder.
  while (files.hasNext()) {
    const file = files.next();

    // Skip anything not tagged as final — leave drafts in place.
    if (!file.getName().toLowerCase().includes(FINAL_TAG)) continue;

    // Find or create this month's archive folder, e.g. "2026-01".
    const monthName = Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM');
    const monthFolder = getOrCreate(DriveApp.getFolderById(ARCHIVE_ROOT), monthName);

    // Move the finished Doc into the dated folder.
    file.moveTo(monthFolder);
    moved++;
  }
  Logger.log('Archived ' + moved + ' finalised Doc(s).');
}

/**
 * Returns the named sub-folder of `parent`, creating it if it does not
 * already exist.
 */
function getOrCreate(parent, name) {
  const it = parent.getFoldersByName(name);
  return it.hasNext() ? it.next() : parent.createFolder(name);
}

How it works

  1. archiveFinalisedDocs opens the active folder and gets an iterator over every file inside it.
  2. For each file it checks the name, lower-cased, for the [final] tag. Anything without the tag is skipped, so live drafts stay put.
  3. For a tagged Doc it builds the current month’s folder name with Utilities.formatDateyyyy-MM, so January 2026 becomes 2026-01.
  4. getOrCreate looks for a sub-folder of that name under the archive root. If one exists it reuses it; if not it creates it. The first finalised Doc of the month makes the folder, and the rest drop into the same one.
  5. file.moveTo relocates the Doc into the dated folder. In Drive a file has one set of parents, so this removes it from the active folder in the same step.

Example run

Suppose the active folder holds these four Docs when the script runs in January 2026:

Doc nameTagged final?Result
Q1 proposal — Harbour Co [final]yesmoved to 2026-01
Service overview draftnostays in active folder
Case study — Lumen Ltd [FINAL]yesmoved to 2026-01
Onboarding guide v3nostays in active folder

After the run, the archive root contains a 2026-01 folder with the two finished Docs, and the active folder is down to the two live drafts.

Trigger it

Run this once a day so finished Docs file themselves away:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose archiveFinalisedDocs, event source Time-driven, type Day timer, and set a quiet hour such as 3am to 4am.
  4. Save, and approve the Drive authorisation prompt on the first run.

Watch out for

  • The Doc is filed by the date the script runs, not the date the work finished. Tag a Doc [final] in late January but let the run slip to February, and it lands in 2026-02. Run daily to keep this tight.
  • The match is a plain substring check. Any Doc with [final] in its name is archived — including a draft sloppily named near-final notes. Keep the tag distinctive.
  • formatDate uses the GMT timezone here. Near midnight that can put a Doc in the previous or next day’s month. Swap GMT for your own timezone string if that matters.
  • getFiles() returns every file type in the folder, not just Docs. A Sheet or PDF named with [final] would also be moved — narrow it with getFilesByType(MimeType.GOOGLE_DOCS) if the folder is mixed.
  • Moving a Doc does not change its sharing. Anyone with a direct link still reaches it; only people who browsed to it via the active folder lose the path.

Related