Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9788

Get list of files from Google drive into google sheets based on date range using app scripts

$
0
0

Hello I am looking for a script to get list of files from google drive within a specific date range.I have got a script on this site here:

List new files from a specific Drive folder and subfolders in Sheets

Credits for the code : User Tedinoz

Code

    function ListAllFilesandFolders() {  // List all files - analyse dates in query/filter  /* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293  Updated since DocsList is deprecated  https://ctrlq.org/code/19854-list-files-in-google-drive-folder  */  // List all files and sub-folders in a single folder on Google Drive  // declare the folder name  var foldername = "<<Enter your folder name>>";  var sheetname = "<<Enter your output sheet name>>";  // declare this sheet  var ss = SpreadsheetApp.getActiveSpreadsheet();  var sheet = ss.getSheetByName(sheetname);  // clear any existing contents  sheet.clear();  // append a header row  sheet.appendRow(["Folder","File Name", "Date Created","Date Last Updated", "Size", "URL", "ID", "Description", "Type"]);  // 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 = folders.next();  // declare an array to push data into the spreadsheet  var data = []; //individuals fields in records  var newdata= []; // whole records  // list files in this folder  // myfiles is a File Iterator  var myfiles = foldersnext.getFiles();  // loop through files in this folder  while (myfiles.hasNext()) { //start loop through main folder    var myfile = myfiles.next();       var fname = myfile.getName();    var fcreate = myfile.getDateCreated();    var fdate = myfile.getLastUpdated();     var fsize = myfile.getSize();    var furl = myfile.getUrl();    var fid = myfile.getId();    var fdesc = myfile.getDescription();    var ftype = myfile.getMimeType();    // Populate the array for this file    data = [       foldersnext,      fname,      fcreate,      fdate,      fsize,      furl,      fid,      fdesc,      ftype    ];    // push the record onto the array    newdata.push(data);  } // end while loop through main folder  // Now get the subfolders  // subfolders is a Folder Iterator  var subfolders = foldersnext.getFolders();  // now start a loop on the SubFolder list  while (subfolders.hasNext()) {// start loop of subfolders    var subfolderdata = [];    var mysubfolders = subfolders.next();    var mysubfolder = mysubfolders.getName();        // Get the files    // mysubfiles is a File Iterator    var mysubfiles = mysubfolders.getFiles();    // now start a loop on the files in the subfolder    while (mysubfiles.hasNext()) { // start 'while' loop of files      var smyfile = mysubfiles.next();      var sfname =  smyfile.getName();      var sfcreate =  smyfile.getDateCreated();      var sfdate =  smyfile.getLastUpdated();       var sfsize =  smyfile.getSize();      var sfurl =  smyfile.getUrl();      var sfid =  smyfile.getId();      var sfdesc =  smyfile.getDescription();      var sftype =  smyfile.getMimeType();      subfolderdata = [         (foldersnext+"/"+mysubfolder),        sfname,        sfcreate,        sfdate,        sfsize,        sfurl,        sfid,        sfdesc,        sftype      ];      newdata.push(subfolderdata);   }// Completes while listing of the files in the sub-folder  } // end loop in the sub-folders  // get the number of records in the array  var newdataLen = newdata.length;  var outputrange = sheet.getRange(2, 1, newdataLen,9)  outputrange.setValues(newdata);  return;} // end of function

The app script should only find new files that are added within a time span like, startDatevalue and endDatevalue

When I am trying to run the code the system gives the below error.

Exception: Cannot retrieve the next object: iterator has reached the end. ListFilesandFoldersDateComparison @ Codegs 33:

which is var foldersnext = folders.next();

Can someone please advise how to resolve this?


Viewing all articles
Browse latest Browse all 9788
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>