Scriptdrill

Gmail, Drive and Calendar Updated November 2026

List files in Drive

DriveApp.getFolderById(id).getFiles() returns an iterator: loop while (files.hasNext()) { const file = files.next(); } and read file.getName(), getUrl(), getId(), getSize(), getLastUpdated(), getMimeType(). Collect rows in an array and write them to the sheet with setValues. getFolders() iterates subfolders the same way.

Syntax

const files = folder.getFiles(); while (files.hasNext()) { const file = files.next(); }
PartWhat it is
DriveApp.getFolderById(id)Folder from the id in its URL; getFoldersByName(name) searches
folder.getFiles() / getFilesByType(mime)Iterators over files, optionally filtered by MIME type
DriveApp.searchFiles(query)Drive search syntax, for example title contains 'report' and modifiedDate > '2026-01-01'
MimeTypeConstants like MimeType.PDF, MimeType.GOOGLE_SHEETS

Examples

const folder = DriveApp.getFolderById("1Fo...");
const files = folder.getFiles();
const rows = [];
while (files.hasNext()) {
  const f = files.next();
  rows.push([f.getName(), f.getUrl(), f.getLastUpdated(), f.getSize()]);
}
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, 1, 4).setValues([["Name", "URL", "Updated", "Bytes"]]);
if (rows.length) sheet.getRange(2, 1, rows.length, 4).setValues(rows);

Result: A table of every file in the folder with link, last update and size

const pdfs = folder.getFilesByType(MimeType.PDF);
while (pdfs.hasNext()) Logger.log(pdfs.next().getName());

Result: Names of the PDF files only

function listAll(folder, path, rows) {
  const files = folder.getFiles();
  while (files.hasNext()) { const f = files.next(); rows.push([path + "/" + f.getName(), f.getUrl()]); }
  const subs = folder.getFolders();
  while (subs.hasNext()) { const s = subs.next(); listAll(s, path + "/" + s.getName(), rows); }
  return rows;
}
const rows = listAll(folder, folder.getName(), []);

Result: Every file in the folder tree with its path, for example Reports/2026/Q3.pdf

const found = DriveApp.searchFiles("title contains 'Budget' and mimeType = 'application/vnd.google-apps.spreadsheet'");
while (found.hasNext()) Logger.log(found.next().getName());

Result: Names of spreadsheets with Budget in the title, anywhere in your Drive

Common mistakes

  • files.forEach is not a function: getFiles returns an iterator, not an array; use while (hasNext()) or push into an array first.
  • The script finds no files in a shared drive folder: use the folder id of the shared drive folder and make sure the running account is a member; DriveApp works with shared drives you can access.
  • Thousands of files exceed the execution time: store the continuation token (files.getContinuationToken()) in PropertiesService and resume on the next run.
  • getLastUpdated writes as a number in the sheet: format the column as a date, or use Utilities.formatDate for text.

Practice List files in Drive

These drills check your answer on live data, free, no account needed.

  • Flatten a nested objectFlatten nested plain objects into one level with dot-separated keys: `{ a: { b: 1 } }` becomes `{ "a.b": 1 }`. Only plain objects are opened up; arrays, dates, `null` and other values are kept as they are. An empty nested object adds no keys.
  • Sort by several keysReturn a sorted copy of `items`. `keys` lists field names in priority order; a leading minus sign, like `"-salary"`, sorts that field from high to low. Numbers compare as numbers and strings with `<` and `>`. Items that tie on every key keep their original order. Do not change `items`.
  • Split into chunksSplit `arr` into arrays of `size` items each. The last chunk holds whatever is left, so it can be shorter. `size` is at least 1. Do not change `arr`.

Questions

How do I get a folder's id?

Open the folder in Drive; the id is the last part of the URL after folders/. Or search by name with DriveApp.getFoldersByName(name).next().

How do I move or rename a file?

file.moveTo(folder) moves it; file.setName(newName) renames it; file.setTrashed(true) deletes it to the trash.

Related

  • Create a Google Doc from a templateGenerate Google Docs from a template with Apps Script: copy the template with DriveApp, replace {{placeholders}} with sheet values via DocumentApp, make PDFs.
  • Gmail search and labelsAutomate Gmail with Apps Script: GmailApp.search with Gmail query syntax, loop threads and messages, read subject, sender and body, apply labels, archive.
  • setValue and setValuesWrite data to Google Sheets with Apps Script: setValue for one cell, setValues for a 2D array that must match the range size, and the dimension error fix.
  • PropertiesServiceStore settings and secrets in Google Apps Script with PropertiesService: script, user and document properties, setProperty and getProperty, JSON values, keys.

Previous: Create a Google Doc from a templateNext: Create calendar events