Retrieving Total File Sizes in Specific Folder of Google Drive using Google Apps Script

Gists

This is a sample script for retrieving the total file sizes in the specific folder of Google Drive using Google Apps Script.

There is a case where you want to retrieve the total file sizes in the specific folder of Google Drive using Google Apps Script. In this post, I would like to introduce a sample script for achieving this.

Sample script

Before you use this script, please enable Drive API at Advanced Google services. And please install FilesApp of a Google Apps Script library.

function myFunction() {
  const folderId = "###"; // Please set your folder ID.
  const res = FilesApp.createTree(folderId, null, "files(size)").files.reduce(
    (o, { filesInFolder }) => {
      filesInFolder.forEach(({ size }) => {
        if (size) {
          o.totalSize += Number(size);
          o.filesWithSize++;
        } else {
          o.filesWithoutSize++;
        }
      });
      return o;
    },
    { filesWithSize: 0, filesWithoutSize: 0, totalSize: 0 }
  );
  console.log(res);
}

Testing

When this script is run, the following result is obtained.

{
  "filesWithSize": 100,
  "filesWithoutSize": 5,
  "totalSize": 123456789
}

 Share!