Home > Enterprise >  How to get Latest files of different names from Google Drive using Google Apps Script?
How to get Latest files of different names from Google Drive using Google Apps Script?

Time:11-05

I receive 2 XML files named call log and sms log every hour. I want to get the ids of the latest version of both call (file name) and sms (file name) files using Google Apps Script. I found this question but this is to just get the single latest file so my question is NOT a duplicate of this one. However, my requirement is a bit more complicated than this.

Below is the code that will get you the latest file:

function NewestFile()
{

var folder = DriveApp.getFolderById('###');
var fi = folder.getFiles();
var lastUpdatedFile = null;

while(fi.hasNext()){
 if(!lastUpdatedFile){ //Checks if we didn't assign anything to lastUpdatedFile
   lastUpdatedFile = fi.next();
 }else{
  var currentFile = fi.next();
  if(lastUpdatedFile.getLastUpdated() < currentFile.getLastUpdated()){
    lastUpdatedFile = currentFile;
  }
 }
}
   
     return [lastUpdatedFile.getId(),lastUpdatedFile.getName()];
}

For example files in google Drive (newest file to oldest - left to right):

call file1 | sms File1 | call file2 | sms file2

The above script will get call file1, but I want the latest of call and sms file both, like this:

latest of call: call file1
latest of sms : sms file1

Let me rephrase it. The above script works fine if you want the latest between all of the files. There are 2 types of files (call,sms). I want to scan all call files (files name starting with the 'call') and get the id of the latest call file. In the same way, scan all the sms files (files name starting with the name 'sms') and get the id of the latest sms file. Please let me know if it is clear. Thank you

CodePudding user response:

Get Latest Files

function getLatestFiles() {
  const ss = SpreadsheetApp.getActive();
  const calls = DriveApp.getFilesByName("Call Log");
  const cA = [];
  const sA = [];
  while(calls.hasNext()) {
    let file = calls.next();
    cA.push( {id:file.getId(),date:file.getDateCreated()})
  }
  cA.sort((a,b)=> {return new Date(b.date).valueOf() - new Date(a.date).valueOf(); })
  const sms = DriveApp.getFilesByName("SMS Log");
  while(sms.hasNext()) {
    let file = sms.next();
    sA.push({id:file.getId(),date:file.getDateCreated()})
  }
  sA.sort((a,b) => {return new Date(b.date).valueOf() - new Date(a.date).valueOf(); });
  Logger.log('Latest Call Log Id: %s',cA[0].id);
  Logger.log('Latest SMS Log Id: %s',sA[0].id);
}

Version 2:

function getLatestFiles() {
  const folder = DriveApp.getFolderById("folderId");
  const files = folder.getFiles();
  const cA = [];
  const sA = [];
  while (files.hasNext()) {
    let file = files.next();
    if (file.getName().match(/call-\(##.*##\)/)) {
      cA.push({ id: file.getId(), date: file.getDateCreated() });
    } else if (file.getName().match(/sms-\(##.*##\)/)) {
      sA.push({ id: file.getId(), date: file.getDateCreated() });
    }
    cA.sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf());
    sA.sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf());
    Logger.log('Latest Call Log Id: %s', cA[0].id);
    Logger.log('Latest SMS Log Id: %s', sA[0].id);
  }
}

Last Version may take some debugging time since I have not received a response for my last question about unique ids and so regex is not tested

  • Related