1

I want to use a script, that deletes AVIs and JPGs files from a specific folder. I want to filter them by date and extension. I have this script, which I think is really close to what I want, but it didn't deletes anything, it sends me an empty letter. (I know, I should comment out the trash parts, but it is for safety reasons so I will do it when my reports would look good)

function DeleteMyAVIs() {
var pageSize = 5000;
var files = null;
var token = null;
var i = null;
var SevenDaysBeforeNow = new Date().getTime()-3600*1000*24*7 ;
Logger.clear()
do {
var result = DocsList.getAllFilesForPaging(pageSize, token);
var files = DocsList.getFolder("motion").getFiles();
var token = result.getToken();
    for(n=0;n<files.length;++n){
      if(files[n].getName().toLowerCase().match('.avi')=='.avi' && files[n].getDateCreated().getTime() < SevenDaysBeforeNow){
            //files[n].setTrashed(true)
            Logger.log(files[n].getName()+' created on '+Utilities.formatDate(files[n].getDateCreated(), 'GMT','MMM-dd-yyyy'))
        }
        if(files[n].getName().toLowerCase().match('.mpg')=='.mpg' && files[n].getDateCreated().getTime() < SevenDaysBeforeNow){
            //files[n].setTrashed(true)
          Logger.log(files[n].getName()+' created on '+Utilities.formatDate(files[n].getDateCreated(), 'GMT','MMM-dd-yyyy'))
        }
      }    
 } while (files.length == pageSize);

  MailApp.sendEmail('xy@gmail.com', 'Script AUTODELETE report', Logger.getLog());

}

tshepang
  • 12,111
  • 21
  • 91
  • 136
Roltii
  • 13
  • 2

1 Answers1

0

You're not getting the files from the folder (it's fixed on the code below). Also, I recommend you get the folder by the id, which is a way more robust, because it allows you to rename the folder or move it inside others, and the code would still work.

Your match is also wrong (although it's not the reason it's not working), because the string will be converted into a regexp. And ".avi" would get any file with "avi" in it anywhere (aside from the very first 3 letters).

Lastly, DocsList token is not useful, because you cannot save it for a later execution, and we page not due to a Google Drive limitation, but Apps Script 6 minutes maximum execution time. In your case of deleting files, continuing the search is not really required since the files will not be there on the next search results anyway.

Lastly, when you're calling formatDate and passing GMT you're most likely going to shift the days by one, either one day before or after depending on where you are, unless you're really on GMT 0 hour and have no daylight saving shift (which I doubt). You should use your own real timezone or grab your script's default (like shown below).

function deleteMyAVIs() {
  var pageSize = 500; //be careful with how much files you process at once, you're going to timeout
  var sevenDaysAgo = Date.now()-1000*60*60*24*7;
  var TZ = Session.getScriptTimeZone();

  Logger.clear();

  var result = DocsList.getFolderById('folder-id').getFilesForPaging(pageSize);
  var files = result.getFiles();
  //token = result.getToken(); //not useful and fortunately not important for your case
  for( n=0;n<files.length;++n ) {
    if(files[n].getName().toLowerCase().match('\\.(avi|mpg)$') && files[n].getDateCreated().getTime() < sevenDaysAgo){
      //files[n].setTrashed(true)
      Logger.log(files[n].getName()+' created on '+Utilities.formatDate(files[n].getDateCreated(), TZ,'MMM-dd-yyyy'))
    }
  }

  MailApp.sendEmail('xy@gmail.com', 'Script AUTODELETE report', Logger.getLog());
}

By the way, if you ever require to continue a Drive search later on, you should use DriveApp instead of DocsList. Please see this other post where I show how to do it.

Community
  • 1
  • 1
Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
  • Thanks for your quick help, it works fine! I will try to convert it to DriveApp later instead of DocsList as you mentioned! – Roltii May 26 '14 at 14:22
  • When using `DriveApp` you can do a more advanced search passing all the criteria you want (file extension and date) to list only the files you really want to delete. Making your script perform much better and again, relieving you from having to deal with continuation tokens. – Henrique G. Abreu May 26 '14 at 15:05