Google Apps Script: using continueFileIterator() function
Saving and using the continuation token from a fileIterator after calling 'DeriveApp.getFiles()' is a good way to continue your batch processing if it gets interrupted. There are many instances when a long batch process that manipulates files might time-out or get interrupted. This allows your process to pickup where you left off when it was interrupted. Of course it is not a substitute for file processing that has to be exactly managed, but it is a good quick solution to resume where your process might have terminated.
These tokens are valid for approximately one week. But there is no way of knowing for sure. Clearing your browser cache, might completely clear the token also. If your batch processing takes minutes and/or hours, saving this continuationToken string avoids restarting your process from the beginning.
You start with creating a fileIterator and saving a continuationToken by calling the function 'getContinuationToken()'. The function returns a string that you can save in your PropertiesService by calling setProperty() or to your database to be retrieved for later use. The continuation token string can be used to resume iteration with the items that remained in the iterator when the token was generated.
Let's explore the following method 'myContinuationToken()' to see how it can be used. We start by calling 'DriveApp.getFiles()' that retrieves a fileIterator for all the files in your 'Google Drive'. From the fileIterator we call a function 'getContinuationToken()' that gets the string to be used later to resume our fileIterator. At this point we can remove our original fileIterator and use the string to create a new fileIterator from the saved string continuationToken. Once we have our continuationToken string, lets create a new fileIterator by calling DriveApp.continueFileIterator(continuationToken) function. Since continueFileIterator() returns a fileIterator, we continue using it as any fileIterator to get the next file(s) to process.
function myContinuationToken() {
var continuationToken;
var filesIterator;
var filesFromContinuationToken;
var nextFile;
//Get all files in your Google Drive
filesIterator = DriveApp.getFiles();
continuationToken = filesIterator.getContinuationToken();
//reset fileIterator, so that we can switch to 'continuationToken' instead
filesIterator = null;
//Pause the code
Utilities.sleep(5000);
//create new filesIterator from 'continuationToken'
filesFromContinuationToken = DriveApp.continueFileIterator(continuationToken);
while (filesFromContinuationToken.hasNext()) {
nextFile = filesFromContinuationToken.next();
Logger.log(nextFile.getName());
};
};