1
votes

I'm trying to delete some files from my drive using this:

function deleteFiles(){
var files = DriveApp.searchFiles('title:720p');
  Logger.log(files);
  //Logger.log(files.next().getName);
  while (files.hasNext()) {
   // files.next().setTrashed(true);
    //Logger.log(files.next().getName());
    Logger.log('hola');
  }
}

But i get Error: Invalid argument:q in all commented lines, even in the while loop. The first logger shows Folder Iterator, so every should be fine...

Files exist! and all have different names, but they have in common "720p" in their names.

Besides, and i haven't been able to implement this yet, i only need to delete these files that are only in one specific folder.

5

5 Answers

1
votes

It appears your searchterm is in the middle or at the end of the file name or title. Try using this, it seems to work for me. https://developers.google.com/drive/v3/web/search-parameters

function deleteFiles() {
  var searchTerm = "12345test"
  var searchFor ="fullText contains '" + searchTerm + "'"; //single quotes are needed around searchterm
 var files = DriveApp.searchFiles(searchFor); 
 while (files.hasNext()) {
   var file = files.next();
   Logger.log(file.getName());
  // file.setTrashed(true)
 }
}
 
1
votes

i found a solution, but it is not efficient, please feel free to provide a more efficient one:

function deleteFiles(){    
  var folder = DriveApp.getFoldersByName('MyFolder').next();
  var files = folder.getFiles();
  while (files.hasNext()) { 
    var file = files.next();
    var filename = file.getName();
    if (filename.indexOf("720p")>-1){
    Logger.log(filename);
    file.setTrashed(true);  
    }     
  }
}
0
votes

could try something like this-

function deleteFiles() {
// Log the name of every file named "12345test" in the user's Drive.
 var files = DriveApp.getFilesByName('12345test');
 while (files.hasNext()) {
   var file = files.next();
   Logger.log(file.getName());
  // file.setTrashed(true)
 }
}
0
votes

Look at the Advanced Drive Service and the Query option "title contains xxxx"

0
votes

here is another way, using searchFiles technique

function deleteFiles() {
  var searchTerm = "12345test"
  var searchFor ="title contains '" + searchTerm + "'"; //single quotes are needed around searchterm
 var files = DriveApp.searchFiles(searchFor); 
 while (files.hasNext()) {
   var file = files.next();
   Logger.log(file.getName());
  // file.setTrashed(true)
 }
}