Google apps script to delete old files permanently

2019-08-24 13:15发布

I created a folder in my root google Drive that contains video files (.avi). I need to write a google apps script to delete the old video files permanently when the total numbers of the files are more than 100 files? i.e deleting all video files except last (newer) 100 files.

The name for each file is related to the time that this file were created example: 2013-02-25__20-29-45-01.avi

2013-02-25__20-24-49-09.avi

2013-02-25__18-26-24-08.avi

......

So I think the script should first list these files alphabetical starting with the newer and ended with the old one, then keep first 100 files and delete all others permanently. I know how to do that in bash script, but not in google drive which I think they use javascript (.gs).

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-24 13:36

As I said in the comments, the script you referred to was not very far from what you want... but I admit your situation is a bit more complex so let's say this will be another exception to sto politics ;-)

That said, I didn't test this code thoroughly so it will probably need some tuning. I left a couple of commented logs throughout the script to test intermediate results, don't hesitate to use them. Also, think about updating the mail adress and don't forget that setTrashed can be manually reversed ;-) (better so when trying new code)

EDIT : I took some time this morning to test the script, it had a couple of "approximations";-) here is a "clean" version that works nicely

function DeleteMyOldAvi() {
    var pageSize = 200;
    var files = null;
    var token = null;
    var i = null;
    var totalFiles = []
    var toDelete = []
    Logger.clear()

    do {
    var result = DocsList.getAllFilesForPaging(pageSize, token);
    var files = result.getFiles()
    var token = result.getToken();
        for(n=0;n<files.length;++n){
            if(files[n].getName().toLowerCase().match('.avi')=='.avi'){
              totalFiles.push([files[n].getName(),files[n].getDateCreated().getTime(),files[n].getId()]);// store name, Date created in mSec, ID in a subarray
//            Logger.log(files[n].getName()+' created on '+Utilities.formatDate(files[n].getDateCreated(), 'GMT','MMM-dd-yyyy'))
            }
          }    
     } while (files.length == pageSize);// continue until job is done

     totalFiles.sort(function(x,y){ // sort array on milliseconds date created (numeric/descending)
                var xp = x[1];
                var yp = y[1];
                return yp-xp ;
                 });
//     Logger.log(totalFiles.length)

       if(totalFiles.length>100){

        for(nn=totalFiles.length-1;nn>=100;nn--){
          toDelete.push(totalFiles[nn]) ;// store the files to delete
           }
//       Logger.log(toDelete)

        for(n=0;n<toDelete.length;++n){
          var file = toDelete[n]
          DocsList.getFileById(file[2]).setTrashed(true);// move to trash each file that is in the toDelete array
          Logger.log(file[0]+' was deleted');// log the file name to create mail message
          }
          MailApp.sendEmail('myMail@gmail.com', 'Script AUTODELETE report', Logger.getLog());// send yourself a mail
       }else{
          MailApp.sendEmail('myMail@gmail.com', 'Script AUTODELETE report', 'No file deleted');// send yourself a mail
       }
 } 
查看更多
登录 后发表回答