appscript.dev
Automation Beginner Drive

Auto-organize new files into folders by type

Sort Northwind's Drive uploads into a tidy folder tree by MIME type.

Published Jun 22, 2025

Northwind has a single inbox/ folder in Drive where everything lands — scanned PDFs, screenshots, exported docs and sheets, all dumped together. Within a week it is an unsearchable pile, and finding last month’s contract means scrolling past a hundred unrelated screenshots. Nobody enjoys tidying it, so nobody does.

This script sweeps the inbox on a timer and moves each file into a destination folder chosen by its MIME type — PDFs to one folder, images to another, Google Docs and Sheets to theirs. It is a beginner-friendly automation: a lookup table of types and a single loop. The inbox stays empty, and everything ends up where it belongs.

What you’ll need

  • A Drive folder that acts as the inbox, and its folder ID from the URL.
  • A destination folder for each file type you want to sort — PDFs, images, Docs, Sheets — each with its own ID.
  • Edit access to all of those folders for the account running the script.

The script

// The folder to sweep — Drive > Northwind > inbox/.
const INBOX = '1abcInboxFolderId';

// MIME type -> destination folder ID.
// Add a row for any file type you want routed.
const ROUTES = {
  'application/pdf': '1abcPdfsFolderId',
  'image/png': '1abcImagesFolderId',
  'image/jpeg': '1abcImagesFolderId',
  'application/vnd.google-apps.document': '1abcDocsFolderId',
  'application/vnd.google-apps.spreadsheet': '1abcSheetsFolderId',
};

/**
 * Moves every file in the inbox folder into the destination folder
 * that matches its MIME type. Files with no matching route are left
 * in place.
 */
function organiseInbox() {
  const inbox = DriveApp.getFolderById(INBOX);
  const files = inbox.getFiles();

  let moved = 0;
  let skipped = 0;

  // 1. Walk every file currently in the inbox.
  while (files.hasNext()) {
    const file = files.next();

    // 2. Look up a destination by the file's MIME type.
    const destId = ROUTES[file.getMimeType()];

    // 3. No route for this type — leave the file where it is.
    if (!destId) {
      skipped++;
      continue;
    }

    // 4. Move the file into its destination folder.
    file.moveTo(DriveApp.getFolderById(destId));
    moved++;
  }

  Logger.log('Moved ' + moved + ' files, left ' + skipped + ' unrouted.');
}

How it works

  1. organiseInbox opens the inbox folder by ID and gets an iterator over the files directly inside it.
  2. For each file it reads the MIME type — application/pdf, image/png, and so on — and looks it up in the ROUTES table.
  3. If the type is not in the table, the file is counted as skipped and left in the inbox, so an unexpected file type is never moved somewhere wrong.
  4. If a destination is found, moveTo relocates the file into that folder. Because moveTo moves rather than copies, the file leaves the inbox in the same step.
  5. At the end it logs how many files were moved and how many were left, so a timer-run can be checked from the execution log.

Example run

The inbox holds five freshly dropped files:

FileMIME typeMoved to
contract-signed.pdfapplication/pdfPDFs folder
screenshot.pngimage/pngImages folder
logo.jpgimage/jpegImages folder
Q2 plan (Google Doc)...apps.documentDocs folder
notes.txttext/plain(left in inbox)

After the run the inbox holds only notes.txt, because plain text has no route.

Trigger it

Run this on a timer so the inbox is swept without anyone thinking about it:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose organiseInbox, event source Time-driven, and a Minutes timer every 15 minutes.

Watch out for

  • A file mid-upload may be picked up before it finishes. A 15-minute interval makes that rare; if it bites, skip files whose getLastUpdated() is within the last minute or two.
  • Only the MIME types listed in ROUTES are moved — everything else stays in the inbox and is re-checked every run. That is intentional, but it means an unrouted file lingers until you add a route for it.
  • moveTo removes the file from the inbox and adds it to the destination. If the script account lacks edit access to either folder, the move fails for that file and the run stops with an error.
  • The iterator only sees files directly in the inbox, not files inside sub-folders. If uploads can land in nested folders, the script will not reach them.
  • Two image types route to the same folder. To split PNGs and JPEGs, point each MIME type at a different folder ID in ROUTES.

Related