Skip to content

Instantly share code, notes, and snippets.

@justinTM
Created December 14, 2017 09:09
Show Gist options
  • Save justinTM/3ddc1cfafb96696885f72ce6b14711b8 to your computer and use it in GitHub Desktop.
Save justinTM/3ddc1cfafb96696885f72ce6b14711b8 to your computer and use it in GitHub Desktop.
Collecting a list of subfolder names using Google Apps Script - a try-catch "hack" for increasing execution time
function GetSubfolderNames(rootFolderID) {
var rootFolder = DriveApp.getFolderById(rootFolderID);
var subfolders = rootFolder.getFolders();
var subfolderName;
var subfolderNames = [];
for (var i=0; ; i++) {
try {subfolderName = subfolders.next().getName();}
catch(e){
if (e == "Exception: Cannot retrieve the next object: iterator has reached the end.")
Logger.log("Got all subfolders");
else {Logger.log(e);}
break;
}
subfolderNames.push(subfolderName);
if (i%20 == 0) // if var 'i' is a multiple of 20, experiment with this number
Utilities.sleep(300); // Pause. Helps prevent "server errors" from a ton of DriveApp calls
}
return subfolders;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment