Google Apps Script to analyze your Google Drive

This Google Apps Script helps you analyze your folder structure and provides you with valuable insights, whether the folders are located in “My Drive”, “Shared drives” or “Shared with me”.

It calculates and displays the size of the folder, the number of files it contains, the number of subfolders, and the maximum depth of your folder hierarchy.

These metrics can be useful to organize and optimize your folder structures or can be essential before moving a folder structure into a shared drive, as they come with some limitations (a maximum of 400,000 items & up to 20 levels of nested folders).

How to run the script

  1. Create a new Google Apps Script via your Google Drive or by going to

  2. Copy the script from below and paste it into the editor, replacing any existing code.

  3. Give your project a name by clicking on “Untitled project” at the
    top-left and entering a name for your script (e.g., “Drive Folder
    Info”). Click the floppy disk icon to save your script.

  4. If you want to retrieve information from a specific folder, just replace root with the specific folder ID.

  5. Click the triangular “Run” button to execute the selected “getFolderInfo” function.

  6. The script will output the folder size, the number of files, the number of folders, and the maximum folder depth in the execution log.
getFolderInfo Execution Log
Script output after completion.

Warning: Google Apps Script imposes runtime restrictions. For Gmail accounts, the maximum execution time for a single script run is capped at 6 minutes, while for Workspace accounts, it extends to 30 minutes. If a script takes longer than this, it will be terminated.


// Function to get information about a Google Drive folder
function getFolderInfo(folderId) {
  // Get the root folder of Google Drive
  var folder = DriveApp.getFolderById("root");
  // Calculate folder information and maximum folder depth
  var fileInfo = calculateFolderInfo(folder);
  var maxDepth = getMaxFolderDepth(folder); // Calculate maximum folder depth
  // Convert bytes to human-readable format
  var sizeFormatted = formatBytes(fileInfo.size);
  // Log the folder information
  Logger.log("Folder size: " + sizeFormatted);
  Logger.log("Number of files: " + fileInfo.numFiles);
  Logger.log("Number of folders: " + fileInfo.numFolders);
  Logger.log("Maximum folder depth: " + maxDepth);
  // Return the folder information as an object
  return {
    size: sizeFormatted,
    numFiles: fileInfo.numFiles,
    numFolders: fileInfo.numFolders,
    maxDepth: maxDepth

// Function to calculate information about a folder (size, number of files, number of subfolders)
function calculateFolderInfo(folder) {
  var fileInfo = {
    size: 0,
    numFiles: 0,
    numFolders: 0

  // Get all files in the folder
  var files = folder.getFiles();
  while (files.hasNext()) {
    var file =;
    fileInfo.size += file.getSize(); // Add file size to total size
    fileInfo.numFiles++; // Increment file count

  // Get all subfolders in the folder
  var subfolders = folder.getFolders();
  while (subfolders.hasNext()) {
    var subfolder =;
    var subfolderInfo = calculateFolderInfo(subfolder); // Recursive call for subfolders
    fileInfo.size += subfolderInfo.size; // Add subfolder size to total size
    fileInfo.numFiles += subfolderInfo.numFiles; // Increment file count
    fileInfo.numFolders += subfolderInfo.numFolders + 1; // Add 1 for the current subfolder

  return fileInfo; // Return the calculated folder information

// Function to calculate the maximum folder depth within a folder hierarchy
function getMaxFolderDepth(folder) {
  var maxDepth = { depth: 0 }; // Initialize max depth as 0
  calculateMaxFolderDepth(folder, 0, maxDepth); // Calculate the maximum folder depth
  return maxDepth.depth; // Return the maximum folder depth

// Recursive function to calculate the maximum folder depth
function calculateMaxFolderDepth(folder, depth, maxDepth) {
  var subfolders = folder.getFolders();
  while (subfolders.hasNext()) {
    var subfolder =;
    calculateMaxFolderDepth(subfolder, depth + 1, maxDepth); // Increment depth for subfolders

  if (depth > maxDepth.depth) {
    maxDepth.depth = depth; // Update max depth if needed

// Function to format bytes into a human-readable format (e.g., KB, MB, GB)
function formatBytes(bytes) {
  if (bytes === 0) return '0 Bytes';
  var k = 1024;
  var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  var i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
asterix Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *