Google Apps Script

From Useful Data
Jump to navigation Jump to search

Google Scripts stop executing after 30 minutes. I have not found the setting to increase that.

Here is a useful script for plodding through a Google Drive and listing the documents therein, and its sub-folders. You will not find another script like it because this one works. The documentation on how to work out the path of a file is wrong, as is much of the advice online on how to do it.

function ListNamedFilesandFolders() {

 /* Adapted from Code written by @hubgit
 Updated since DocsList is deprecated
 // List all files and sub-folders in a single folder on Google Drive
 // declare the folder name
 var foldername = 'PUT_FOLDER_NAME_HERE';
 // declare this sheet
 var sheet = SpreadsheetApp.getActiveSheet();
 // clear any existing contents
 // append a header row
 // sheet.appendRow(["Folder","Name", "Date Last Updated", "Size", "URL", "ID", "Description", "Type", "Owner", "Owner2", "Owner3", "Path", "Created"]);
    sheet.appendRow([         "Name", "Date Last Updated",                                     "Type",                              "Path", "Created"]);
 // getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
 // folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
 var folders = DriveApp.getFoldersByName(foldername);
 var foldersnext =;
 // Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
 //Initiate recursive function
 listFilesAndSubfolders(foldersnext, foldername);  


function listFilesAndSubfolders(folder, path) {

 listFiles(folder, path);
 var subfolders = folder.getFolders();
 while (subfolders.hasNext()) {
   var mysubfolders =;
   var mysubfolderName = mysubfolders.getName(); 
   path = path + "/" + mysubfolderName;
   listFilesAndSubfolders(mysubfolders, path);    


function listFiles(foldersnext, path) {

 var data = [];
 var sheet = SpreadsheetApp.getActiveSheet();
 // list files in this folder
 // myfiles is a File Iterator
 var myfiles = foldersnext.getFiles();
 var lastFAllParents = "zzz"
 // loop through files in this folder
 while (myfiles.hasNext()) {
   var myfile =;
   var fname = myfile.getName();
   var fdate = myfile.getLastUpdated(); 
   // speedo  var fsize = myfile.getSize();
   // speedo  var furl = myfile.getUrl();
   // speedo  var fid = myfile.getId();
   // speedo  var fdesc = myfile.getDescription();
   var ftype = myfile.getMimeType();
   // speedo  var fowner = myfile.getOwner();
   // speedo  var fowner2 = fowner.getName();
   // speedo  var fowner3 = fowner.getEmail();
   var fAllParents = myfile.getParents();
   if ( fAllParents != lastFAllParents) {
     lastFAllParents = fAllParents;
     var fpath = ""
     while ( fAllParents.hasNext()) {
       var fNextFolder =;
       var fNextFolderName = fNextFolder.getName();
       fpath = fNextFolderName + "/" + fpath;
       fAllParents = fNextFolder.getParents();
       // speedo  debugger;
   // speedo  debugger;
   var fcreated = myfile.getDateCreated();
   //Logger.log("File Name is "+myfile.getName()); //Logger.log("Date is "+myfile.getLastUpdated()); //Logger.log("Size is "+myfile.getSize());
   //Logger.log("URL is "+myfile.getUrl()); //Logger.log("ID is "+myfile.getId()); //Logger.log("Description is "+myfile.getDescription());
   //Logger.log("File Type is "+myfile.getMimeType());
   // Populate the array for this file
   data = [ 
     // speedo  fsize,
     // speedo  furl,
     // speedo  fid,
     // speedo  fdesc,
     // speedo  fowner,
     // speedo  fowner2,
     // speedo  fowner3,
   //Logger.log("data = "+data); //DEBUG
 } // Completes listing of the files in the named folder