Created
July 27, 2022 09:54
-
-
Save mrowe/9ecc1ae76b763e7de804cfc40bf8873f to your computer and use it in GitHub Desktop.
Get the total size of a folder in Google Drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adapted from https://webapps.stackexchange.com/a/123681 | |
function doCount() { | |
var FOLDER_ID = "<folder_id>"; | |
Logger.log("Getting folder sizes..."); | |
var totalSize = countFolder(DriveApp.getFolderById(FOLDER_ID), "/"); | |
Logger.log("Folder Sizes Report completed"); | |
Logger.log(Utilities.formatString("TOTAL SIZE: %12.2f MB", (totalSize / (1024 * 1024)))); | |
} | |
function countFolder(folder, path) { | |
var totalSize = countFilesIn(folder); | |
var children = folder.getFolders(); | |
while (children.hasNext()) { | |
totalSize += countFolder(children.next()); | |
} | |
return totalSize; | |
} | |
function countFilesIn(folder) { | |
Logger.log("Counting files in " + folder); | |
var filesSize = 0; | |
var files = folder.getFiles(); | |
while (files.hasNext()) { | |
filesSize += files.next().getSize(); | |
} | |
return filesSize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment