Home > Back-end >  G AppScript Search Files
G AppScript Search Files

Time:07-08

I am trying to write a script that will allow me to search within all of google drive, including shared teams and subfolders (Many Subfolders)

With what I wrote I can't find all the files, in fact there is a discrepancy between the ones I see from the web page and the ones the script returns to me

This is just a part of the script, when the file is found, it is moved to a folder. I have several files to move, via browser it is not manageable.

function SearchFiles() {
  //Please enter your search term in the place of Letter
  var searchFor ='title contains "find ME"';
  var names =[];
  //var fileIds=[];
  var files = DriveApp.searchFiles(searchFor);
  while (files.hasNext()) {
    var file = files.next();
    //var fileId = file.getId();// To get FileId of the file
    //fileIds.push(fileId);
    var name = file.getName();
    names.push(name);

  }

  for (var i=0;i<names.length;i  ){
    Logger.log(names[i]);
    //Logger.log("https://drive.google.com/uc?export=download&id="   fileIds[i]);

  }

}

CodePudding user response:

To find all files including the ones on Shared Drives, you need to use the Advanced Drive Service instead of DriveApp

Gets a collection of all files in the user's Drive

  • On the contrary, the Advanced Drive Service based on the Drive API v2 allows you to set the parameters supportsAllDrives and includeItemsFromAllDrives to true and thus obtain results from all Drives
  • To use the Advanced Drive Service, first you need to enable it following the instructions
  • Then, you can use the method Files: list setting the query parameter q to title contains 'find ME'

In Apps Script it could look as following:

function SearchFiles() {
  var searchFor ='title contains "findMe"';
  var files = Drive.Files.list({supportsAllDrives: true, includeItemsFromAllDrives: true, q: searchFor}).items;
  for (var i=0; i<files.length; i  ){
    Logger.log(files[i].title);
  }
}
  • Related