-1

I am trying to figure this script out but no joy.

I am using someones script to delete files after a certain date, but I want to mod it to just delete any .bak files no matter of the date. That bits fine but I keep getting an error:

"TypeError: Cannot find function getAllFilesForPaging in object Drive"

'function DeleteMyJpegs() {
var pageSize = 200;
var files = null;
var token = null;
var i = null;
var ThirtyDaysBeforeNow = new Date().getTime()-3600*1000*24*30 ;// 30 is the number of days 

//(3600 seconds = 1 hour, 1000 milliseconds = 1 second, 24 hours = 1 day and 30 days is the duration you wanted Logger.clear();

do {


var result = DriveApp.getAllFilesForPaging(pageSize, token);
var files = result.getFiles()
var token = result.getToken();
    for(n=0;n<files.length;++n){
        if(files[n].getName().toLowerCase().match('.bak')=='.bak' && files[n].getDateCreated().getTime()<ThirtyDaysBeforeNow){
//            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('email@address.co.uk', 'Script AUTODELETE BAK report', Logger.getLog());

} '

PMAXWELL
  • 1
  • 2
  • Something went wrong with the code – PMAXWELL Feb 16 '16 at 11:42
  • When you copy a code from somewhere and try to adapt it you should make sure you change it correctly. The auto complete feature in the script editor (CTRL+ SPACE) can be a great help to see the possible method you can use... you would have noticed that DocsList and DriveApp have very different approches and therefor different methods. – Serge insas Feb 16 '16 at 16:14

1 Answers1

0

There is no getAllFilesForPaging() method in DriveApp. This may be a custom function written by the original author. This is how you can rewrite this using the DriveApp file iterator.

function deleteMyBAKs(){
  var ThirtyDaysBeforeNow = new Date().getTime()-3600*1000*24*30 ;// 30 is the number of days
  var files = DriveApp.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    if(file.getName().toLowerCase().match('.bak')=='.bak' && file.getDateCreated().getTime()<ThirtyDaysBeforeNow){
      try{file.setTrashed(true);}
      catch(e){Logger.log("Unable to Trash: "+file.getName()) }         
      Logger.log(file.getName()+' created on '+Utilities.formatDate(file.getDateCreated(), 'GMT','MMM-dd-yyyy'));
     }       
   } 
  MailApp.sendEmail('email@address.co.uk', 'Script AUTODELETE BAK report', Logger.getLog());
}

You read more about the DriveApp and file iterators at:
https://developers.google.com/apps-script/reference/drive/drive-app

Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
  • That is great, thank you. It appears to work apart from it saying: – PMAXWELL Feb 16 '16 at 15:39
  • That is great, thank you. It appears to work apart from it saying: file.setTrashed(true) "ACTION NOT ALLOWED" I will have a loo – PMAXWELL Feb 16 '16 at 15:51
  • @Spencer : the code in the question was taken from [one of my answers](http://stackoverflow.com/questions/14628895/automatically-delete-files-in-google-drive), it used originally a DocsList method : `DocsList.getAllFilesForPaging` but the conversion to DriveApp was not good... – Serge insas Feb 16 '16 at 16:10
  • @PMAXWELL Its probably because you only have read access to a file. I added a try..catch example above. Remember to accept if this answers your question. – Spencer Easton Feb 16 '16 at 17:57
  • I would have liked to study the code more but I have 1.5 million files on the drive and it crashed and now won't sync so I had to quickly delete a lot of files out of the drive. I think it is basically there, it seems to run fine. I had 2 issues. 1st it doesn't seem to be able to delete any file "Unable to Trash" on every file. Second it kept timing out so I added a counter so it would run 100 at a time. I am not sure why it's unable to trash anything. I am the owner and admin of the domain. I have looked for hours but no joy – PMAXWELL Feb 17 '16 at 08:39