← What Findest can file

A free script that saves Gmail attachments to Google Drive

Under fifty lines of Google Apps Script, running in your own account. It also clears the backlog of old mail — the one thing Findest does not do.

How do you save Gmail attachments to Google Drive with a script?

Open script.google.com, create a project, paste the script below and press Run on saveAttachments. It copies every attachment matching a Gmail search into a Drive folder named by date, labels each conversation so nothing is copied twice, and can run by itself every hour.

  1. Create a new Apps Script project

    Go to script.google.com while signed in to the Google account whose Gmail you want to export, and choose New project. Everything runs inside that account; nothing is sent to Findest or anyone else.

  2. Paste the script and set three lines

    Replace the sample code with the script below. Change FOLDER_NAME to the Drive folder you want, and SEARCH to any Gmail search — from:supplier.com has:attachment, filename:pdf after:2026/01/01, and so on.

  3. Run saveAttachments and approve access

    Choose saveAttachments in the toolbar and press Run. Google asks you to authorise your own script and warns that it is unverified — expected for a script you wrote yourself. Advanced › Go to project continues.

  4. Make it run every hour

    Run runEveryHour once. From then on the script processes up to 50 new conversations an hour, so a large backlog empties over a day or two and new mail follows automatically.

The script

MIT licence: copy it, change it, share it. The only lines you need to touch are the three under “Settings”.

Code.gs
/**
 * Save Gmail attachments to Google Drive — a free Google Apps Script by Findest.
 * Guide: https://findest.site/archive/save-gmail-attachments-to-drive-script.html
 *
 * Copies every attachment that matches SEARCH into one Drive folder, renamed
 * with the date of the email, and labels each conversation once it is done so
 * it is never copied twice. Runs entirely inside your own Google account.
 */

// Settings — change these three lines.
const FOLDER_NAME = 'Gmail attachments';    // Drive folder, created if it does not exist
const SEARCH = 'has:attachment -in:chats';  // any Gmail search, e.g. 'from:supplier.com has:attachment'
const DONE_LABEL = 'saved-to-drive';        // added to each conversation once processed

const MIN_BYTES = 3 * 1024;  // skips signature images and tracking pixels
const BATCH = 50;            // conversations per run, so each run stays under the time limit

function saveAttachments() {
  const folder = getOrCreateFolder_(FOLDER_NAME);
  const label = GmailApp.getUserLabelByName(DONE_LABEL) || GmailApp.createLabel(DONE_LABEL);
  const threads = GmailApp.search(SEARCH + ' -label:' + DONE_LABEL, 0, BATCH);

  threads.forEach(function (thread) {
    thread.getMessages().forEach(function (message) {
      const day = Utilities.formatDate(message.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
      message.getAttachments({ includeInlineImages: false }).forEach(function (file) {
        if (file.getSize() < MIN_BYTES) return;
        folder.createFile(file.copyBlob()).setName(day + '_' + file.getName());
      });
    });
    thread.addLabel(label);
  });

  Logger.log('Saved attachments from ' + threads.length + ' conversations.');
}

// Run this one once to make saveAttachments run by itself every hour.
function runEveryHour() {
  ScriptApp.newTrigger('saveAttachments').timeBased().everyHours(1).create();
}

function getOrCreateFolder_(name) {
  const folders = DriveApp.getFoldersByName(name);
  return folders.hasNext() ? folders.next() : DriveApp.createFolder(name);
}

What it does not do

  • It copies files, it does not read them. Every attachment matching the search lands in one folder; sorting invoices from delivery notes is still your job, or a rule's.
  • The label goes on the whole conversation. If a reply with a new attachment arrives later in a conversation that is already labelled, that new file is skipped — remove the label to reprocess it.
  • Files under 3 KB are skipped on purpose: signature images and tracking pixels. A genuinely tiny attachment would be skipped too.
  • It runs on Google's schedule, not instantly: hourly at best, and subject to Apps Script's daily quotas on consumer accounts.
Where Findest comes in

The script moves files; it does not know what they are. If you want invoices in one folder, contracts in another and receipts named by merchant, that is the part Findest does — it reads each new attachment, matches it against rules you write as sentences, and files it into Drive, OneDrive or Dropbox. Use the script for the backlog and Findest from here on, or just the script if one folder is enough.

Questions about the script

Is it safe to authorise the script?

It runs as you, inside your own Google account, and its full source is on this page — under fifty lines you can read before running. The “unverified app” screen appears because the script is yours and has not been through Google's review, not because it does anything unusual.

Does it work on old emails?

Yes. It works through everything matching the search, 50 conversations per run, newest first, until the search runs out. That is the difference from Findest, which only files attachments that arrive after you connect.

Can it save into a Shared Drive or a subfolder?

As written it finds or creates a folder by name in My Drive. For a specific folder, replace getOrCreateFolder_(FOLDER_NAME) with DriveApp.getFolderById('…') using the ID from the folder's URL.

How do I stop it?

Open the project, go to Triggers in the left sidebar and delete the hourly trigger. Deleting the project removes it completely; the files it already saved stay in Drive.

Related questions

Download all attachments from Gmail — at once, and from now on

Gmail has a one-click answer for a single message and no answer at all for a hundred of them. Both halves are worth knowing before you install anything. Read it →

Save Gmail attachments to Google Drive, automatically

Gmail routes envelopes. It has never been able to route what is inside the attachment — which is the part you actually want filed. Read it →

Why email filters miss what is inside the attachment

This is not a complaint about Gmail. It is a description of what envelope-level matching can and cannot decide — including the version of it we shipped and had to remove. Read it →

One folder is fine. Sorted folders are better.

Free plan, two rules, no card — enough to see whether it sorts your documents the way you would.