google-apps-script DriveApp Service Get all folders - put folders into a continuation token - then retrieve from token


function processGoogleDriveFolders() {
  var arrayAllFolderNames,continuationToken,folders,foldersFromToken,thisFolder;//Declare variable names
  arrayAllFolderNames = [];//Create an empty array and assign it to this variable name
  folders = DriveApp.getFolders();//Get all folders from Google Drive in this account
  continuationToken = folders.getContinuationToken();//Get the continuation token

  Utilities.sleep(18000);//Pause the code for 3 seconds
  foldersFromToken = DriveApp.continueFolderIterator(continuationToken);//Get the original folders stored in the token
  folders = null;//Delete the folders that were stored in the original variable, to prove that the continuation token is working
  while (foldersFromToken.hasNext()) {//If there is a next folder, then continue looping
    thisFolder =;//Get the next folder
    arrayAllFolderNames.push(thisFolder.getName());//Get the name of the next folder
  Logger.log(arrayAllFolderNames);//print the folder names to the Logs