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
organiseInboxopens the inbox folder by ID and gets an iterator over the files directly inside it.- For each file it reads the MIME type —
application/pdf,image/png, and so on — and looks it up in theROUTEStable. - 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.
- If a destination is found,
moveTorelocates the file into that folder. BecausemoveTomoves rather than copies, the file leaves the inbox in the same step. - 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:
| File | MIME type | Moved to |
|---|---|---|
contract-signed.pdf | application/pdf | PDFs folder |
screenshot.png | image/png | Images folder |
logo.jpg | image/jpeg | Images folder |
Q2 plan (Google Doc) | ...apps.document | Docs folder |
notes.txt | text/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:
- In the Apps Script editor, open Triggers (the clock icon).
- Click Add Trigger.
- 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
ROUTESare 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. moveToremoves 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
Build a recurring file-delivery system
Drop a fresh report file into a Northwind client folder weekly — they don't even ask.
Updated Dec 15, 2025
Build a Drive search index in Sheets
Make Northwind's file metadata searchable in a Sheet — like Spotlight for Drive.
Updated Dec 7, 2025
Build a shared-folder onboarding kit
Auto-grant new Northwind hires the folders they need on day one.
Updated Nov 29, 2025
Route saved email attachments to project folders
File Gmail attachments into the right Northwind client folder based on subject keywords.
Updated Nov 25, 2025
Bundle a folder of images into one PDF
Combine Northwind scans into a single deliverable PDF using a generation service.
Updated Nov 17, 2025