Monday, March 20, 2017

Google Drive Report

If you’re like me, you’ve accumulated a lot of files on your Google Drive and getting a summary might provide some helpful insights. The following Google App Script will output a summary report by Mime type (e.g. pdf, jpeg) with the total number of files and the total number of bytes used, sorted descending so the largest total number of bytes comes first. It also includes a few statistics on the Google Drive usage. You will find that certain Google file types do not count against your space quota and will have a zero file size.

To use this script...
  • Create a Google App Script (New … More … Google App Script) in your Google Drive, delete any code you see, cut and paste the code below into the script, save the script and give it a name of your choosing.
  • Create a new Google document (New … Google Docs) to hold the report, give it a name, type in a few characters (gibberish is fine) and format those characters in the font type and size you want the output report to take.  Copy (CTRL+C) the document id from the URL (the gibberish part that will look something like 1azjbDuyyT7DNvSfKsP9kOugGukF3iVN5lZ9hud47_aU), then save the document.
  • Paste (CTRL+V) the document ID into the Google App Script, replacing the bolded “YOUR FILE ID GOES HERE” on line 8.
  • Run the script (Run … AllDriveFiles or the arrowhead icon).
  • When the script is finished, you can view your report file.

The output (partial example) will look something like this.

Report generated on Tue Mar 14 2017 14:55:25 GMT-0400 (EDT)

Google Drive Storage Used is 3454522976 Bytes (3294.49 MB)
Google Drive Storage Limit is 123480309760 Bytes (117760.00 MB)
Google Drive Storage Percent Used is 2.80%

--------------------------------------------

Type = application/pdf Count = 2256 Size = 2167898741 Bytes (2067.47 MB)
Type = image/jpeg Count = 3525 Size = 1706782169 Bytes (1627.71 MB)
Type = video/mp4 Count = 8 Size = 369721056 Bytes (352.59 MB)
Type = video/quicktime Count = 3 Size = 162988797 Bytes (155.44 MB)
Type = video/x-m4v Count = 1 Size = 33350360 Bytes (31.81 MB)

If this isn’t exactly what you need, I hope it will serve as a useful starting point and reference.

----------------------------- Google App Script Code ----------------------------------------

function AllDriveFiles() {
//  
// Get date to output on the report
var today = new Date();
var todaysdate = new Date(today.getTime() - 1 * 24 * 60 * 60 * 1000);
var date = todaysdate.toDateString();
// Open the output file by its ID
var report = DocumentApp.openById('YOUR FILE ID GOES HERE');
//  
// Remove any text already in the report
report.setText("  ");
//  
// Print the date
str = 'Report generated on ' + today;
report.getBody().appendParagraph(str);
report.getBody().appendParagraph(' ');  
var used = DriveApp.getStorageUsed();
//
// Print amount of Drive space being used
str = 'Google Drive Storage Used is ' + used + ' Bytes (' + (used/1048576).toFixed(2) + ' MB)';
report.getBody().appendParagraph(str);
var limit = DriveApp.getStorageLimit();
//
// Print the total amount of Drive space
str = 'Google Drive Storage Limit is ' + limit + ' Bytes (' + (limit/1048576).toFixed(2) + ' MB)';
report.getBody().appendParagraph(str);
percent = used*100/limit;
//
// Print the percentage of the total space used by Drive - this does not include sources like GMail
str = 'Google Drive Storage Percent Used is ' + percent.toFixed(2) + '%';
report.getBody().appendParagraph(str);
report.getBody().appendParagraph(' ');
report.getBody().appendParagraph('--------------------------------------------');
report.getBody().appendParagraph(' ');
//
// Get all Drive files and store the total file count and total file space by each unique Mime Type
var arrType = [];
var arrCount = [];
var arrSize = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
 var file = files.next();
 arrLen = arrType.length;
 for (i = 0; i < arrLen; i++) {
   if (file.getMimeType() == arrType[i]) {
     arrCount[i]++;
     arrSize[i] = arrSize[i] + file.getSize();
     i = arrLen + 10;
     }
 }
 if (i == arrLen) {
    arrType[i] = file.getMimeType();
    arrCount[i] = 1;
    arrSize[i] = file.getSize();
    }
}
//
// Sort the arrays by descending total file size
arrLen = arrType.length;
var sorted=0;
var i=0;
while (sorted == 0) {
 sorted=1;
 while (i < arrLen-1) {
   if (arrSize[i] < arrSize[i+1]) {
     Type = arrType[i];
     Count = arrCount[i];
     Size = arrSize[i];
     arrType[i] = arrType[i+1];
     arrCount[i] = arrCount[i+1];
     arrSize[i] = arrSize[i+1];
     arrType[i+1] = Type;
     arrCount[i+1] = Count;
     arrSize[i+1] = Size;
     sorted=0;
     i=0;
   }
   else {
     i++;
   }
 }
}
//
// Print each detail line
arrLen = arrType.length;
for (i = 0; i < arrLen; i++) {
 str = 'Type = ' + arrType[i] + ' Count = ' + arrCount[i] + ' Size = ' + arrSize[i] + ' Bytes (' + (arrSize[i]/1048576).toFixed(2) + ' MB)';
 report.getBody().appendParagraph(str);
 }
}

No comments: