0

Using Google Apps Script (GAS), I wrote a program that checks the (parent) folder, and finds all files in it that were uploaded within the past 30 minutes. I am using recursion for this as I don't know how many folders exist in that parent folder and how deep each, and I have to detect all files in there.

var arrRecentFiles = [];
var now = new Date();
function FindRecentlyUploadedFiles(pFolder)
{
    var cFolders = pFolder.getFolders();
    while(cFolders.hasNext())
    {
        var cFolder = cFolders.next();
        var cFiles = cFolder.getFiles();
        while(cFiles.hasNext())
        {
            var cFile = cFiles.next();
            
            //Push only if the file was uploaded within the past 30 minutes
            if( CheckIfRecent(cFile) )
            {
                 arrRecentFiles.push(cfile.getName());
            }
        }
        //Recursion here
        FindRecentlyUploadedFiles(cFolder);
    }
}

/*Check if this cFile was uploaded within the past 30 minutes */
function CheckIfRecent(cFile)
{
     var diff = (now.getTime() - cFile.getDateCreated().getTime()) / (60 * 1000);
     if(diff < 30)
         return true;
     else
         return false;
}

This program runs fine, but when there are many folders, of course it takes time. I wanted to make this faster, but is this possible at all? I tried without recursion, but it just made the code longer and uglier, so I thought I would stick with recursion. But I am not sure if that is the main cause that is slowing the whole process down.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Wang Dang
  • 121
  • 2
  • 13
  • I suggest you to try using the Google Drive Advanced Service. In the meantime you find an answer prepare your script to handle the execution time limit. There are several strategies like storing a token, using parallel processing, among others. – Rubén Aug 14 '20 at 19:31
  • 1
    https://stackoverflow.com/questions/41741520/how-do-i-search-sub-folders-and-sub-sub-folders-in-google-drive – pinoyyid Aug 14 '20 at 21:59

0 Answers0