7
votes

Is it possible to use Google Apps Script to search Google Drive for both documents and folders?

Google have killed their own docs/drive search gadget as it appears to rely on iGoogle and Google Enterprise support have admitted this.

Thank you

2
It is indeed possible... it is also possible to show your attempts.... or not.? Please read the FAQ.Serge insas

2 Answers

8
votes

I think you are looking for SearchFile and SearchFolder of the DriveApp. The full list of parameters is available in the Google Drive SDK documentation

I've run some tests and seems like it's not possible to do 1 search and get files and folders like it's possible calling the search function from the Google Drive API.

Here a code that list the files and folders with a title that have 2013 in it

function myFunction() {
  var searchFor ='title contains "2013"';
  var names =[];
  var files = DriveApp.searchFiles(searchFor);
  while (files.hasNext()) {
    var file = files.next();
    names.push(file.getName());
  }
  var folders = DriveApp.searchFolders(searchFor);
  while (folders.hasNext()) {
    var file = folders.next();
    names.push(file.getName());
  }
  for (var i=0;i<names.length;i++){
    Logger.log(names[i]);
  }

}
2
votes

Try this piece of code

function searchDrive() {
  var folderToSearch = "FolderName";
  var folders = DriveApp.getFoldersByName(folderToSearch);
  Logger.log(folders);

  var fileToSearch = "fileName";
  var files = DriveApp.getFilesByName(fileToSearch);
  Logger.log(files);
}

This example can be found here.