0

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).

Kara
  • 6,115
  • 16
  • 50
  • 57
Linux
  • 325
  • 2
  • 6
  • 17
  • not sure anyone will do the job for you, this is not how sto is supposed to work...[see the faq about that](http://stackoverflow.com/faq) – Serge insas Mar 02 '13 at 00:40
  • To Serge with respect, http://stackoverflow.com/questions/14628895/automatically-delete-files-in-google-drive – Linux Mar 02 '13 at 14:59
  • Yes I know, there are some exceptions...;-) I found the idea interesting for my personal use... btw, this needs only a few modifications to make it work for your use case, give it a try and I'll be happy (or someone else) to help if you get stuck on something. – Serge insas Mar 02 '13 at 15:52
  • Before post my question here, I tried for two days to edit the code in the above link without succeed, therefore I wrote my request here. – Linux Mar 02 '13 at 16:39

1 Answers1

0

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
       }
 } 
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks Serge for your efforts to write the script, I will test it today and return to you. – Linux Mar 03 '13 at 17:11
  • OK,thanks, you solved the first part of my question, it sort the old (avi) files over than 100, but it send them to the trash! not delete them permanently!. I mean I wanted to delete them forever, because sending files to trash will not free some space in the google drive! do you know how to delete them forever? – Linux Mar 03 '13 at 17:56
  • AFAIK there is no direct way in GAS to delete files without sending them to trash nor to empty the trash... I think it would be possible using a call to an external API but I suggest to raise a new question on that subject since I don't know how...(and I'm interested too ;-). In the mean time please consider accepting the answer, it's worth 15 points isn't it ? – Serge insas Mar 03 '13 at 18:22
  • @Sergeinsas we discussed the topic here: https://stackoverflow.com/questions/46564653 – Matteo Ragni Oct 04 '17 at 18:14