0

GOAL: I want to print/log all files and folders that have been only created today by any users from a persons personal GDrive. So normally a coder can do the basics of printing all files and folders in a user GDrive, so I would like to know if it's possible to only print files and folders of that users GDrive BUT only the ones that been have created today.

function CRUD(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  var timezone = ss.getSpreadsheetTimeZone();

  var today     = new Date();
  var oneDayAgo = new Date(today.getTime() - 1 * 24 * 60 * 60 * 1000);
  var startTime = oneDayAgo.toISOString();
  
  var folders = DriveApp.getFolders();
  var files = DriveApp.getFiles();
  
  if(file <= startTime && folder <= startTime){
    while(folders.hasNext() && files.hasNext()){
   var folder = folders.next();
   var file = files.next();
   var ff = ["folder Id ="+folder.getId(),"folder Name ="+folder.getName(),"folder date created="+folder.getDateCreated(),"file Id ="+file.getId(),"file Name ="+file.getName(),"file date created="+file.getDateCreated()];
   console.log(ff);
   
   sheet.appendRow(ff);
   }
  } else{
    console.log("Nothing is working");
  }
}
Adam
  • 63
  • 7

1 Answers1

0

file <= startTime is not a valid comparison

  • Instead you can query for file.getDateCreated().getTime() <= startTime, whereby var startTime = oneDayAgo.getTime(); - getTime() will transform both time values into ms since Unix Epoch which makes them comparable.
  • More on how to compare dates in Javascript

Your if statement queries for file before file has been defined

  • Instead it should go after the line var file = files.next();

As it is, the script only compares dates for a random combination of folders and files

Instead, you probably want to retrieve all files inside of each folder and query for a combination where both a files and the parent folder comply with the startTime condition

Sample how to modify your scipt:

function CRUD(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  var timezone = ss.getSpreadsheetTimeZone();

  var today     = new Date();
  var oneDayAgo = new Date(today.getTime() - 1 * 24 * 60 * 60 * 1000);
  var startTime = oneDayAgo.getTime()//.toISOString();

  var folders = DriveApp.getFolders();
  var files = DriveApp.getFiles();
  Logger.log(startTime);

  while(folders.hasNext()){
    var folder = folders.next();
    if(folder.getDateCreated().getTime() <= startTime){
      var files = folder.getFiles();
      while(files.hasNext()){
        var file = files.next();
        if(file.getDateCreated().getTime() <= startTime ){
          var ff = ["folder Id ="+folder.getId(),"folder Name ="+folder.getName(),"folder date created="+folder.getDateCreated(),"file Id ="+file.getId(),"file Name ="+file.getName(),"file date created="+file.getDateCreated()];
          console.log(ff);

          sheet.appendRow(ff);
        }
        else{
          console.log("Nothing is working");
        }
      }
    }
  }
}

Mind hat if you have many folders and files, the execution could take a while.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • This code is slightly different from what I am trying to achieve, this code above logs and prints out all the files and folders since date creation. I am trying to log and print only files and folders that have been created or modified only today. – Adam Jun 11 '20 at 05:04
  • I see, than instead of `folder.getDateCreated().getTime() <= startTime` you need to query for `folder.getDateCreated().getTime() >= startTime` or `folder.getDateCreated().getTime() > startTime` (same for `file`) - for retrieving files that are less than 24 hours old, see also [Javascript operators](https://www.w3schools.com/js/js_operators.asp). Is this what you want, or do the files need to be created the date of today rather than the last 24 hours? – ziganotschka Jun 11 '20 at 07:12
  • 1
    Thank you, I finally got what I needed. https://www.labnol.org/code/19921-monitor-drive-files-google-script A few modifications here and there. – Adam Jun 11 '20 at 08:05