Leon K.
Leon K.

Reputation: 115

How to use searchFile and searchFolder at the same time correctly? - App Script

The next script what it does is to search all the spreadsheets with a certain name in the drive starting from a certain folder.

function searchSSH(folder, path) { 
  if (folder == null && path == null) {
  return 
  searchSSH(DriveApp.getFolderById("ID"), "");
  }

  var files = [];

  path = path + "/" + folder.getName();  
  var searchFile = "fullText contains 'Project <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
  var fileIterate = folder.searchFiles(searchFile);

  while ( fileIterate.hasNext() ) {
    var file = fileIterate.next();
    var fileId = file.getId();
    var name = file.getName();
    files.push(name);

  for (var i=0; i<files.length; i++){
    Logger.log(files[i]);
  } 
 }

  var folderIterate = folder.getFolders();
  while(folderIterate.hasNext()) {
    var searchFold = searchSSH(folderIterate.next(), path);
    for (var i = 0; i < searchFold.length; i++) {
    files.push(searchFold[i]);
    }     
  }
  return files;    
 }

What I am trying to do is to see how I can do it so that I can also look for a certain folder just like the searchFile does and I try to do it like this...

    function searchSSH() {         
      var Folder =  DriveApp.getFolderById("ID");

      var folders = Folder.searchFolders('fullText contains "project"');
      while (folders.hasNext()) {
        var folder1 = folders.next();
        Logger.log(folder1.getName());
        for (var i = 0; i < folder1.length; i++) {
          files.push(folder1[i]);
        }     
      }

      var files = [];
      var searchFile = "fullText contains 'test <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
      var fileIterate = Folder.searchFiles(searchFile);

      while ( fileIterate.hasNext() ) {
        var file = fileIterate.next();
        var fileId = file.getId();
        var name = file.getName();
        files.push(name);

        for (var i=0; i<files.length; i++){
          Logger.log(files[i]);
        } 
      }       
      return files;    
    }

But doesn´t works. What I'm trying to do is to iterate through all the folders until I find the folder with the name project and keep iterating in that folder until I find all the spreadsheets with the test name but only search in the first folder.

Upvotes: 1

Views: 367

Answers (1)

Tanaike
Tanaike

Reputation: 201428

  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script
  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand

From your this reply, I could understand as follows.

  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.

If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.

Pattern 1:

Flow:

  1. Retrieve files from filename of Test using getFilesByName().
  2. Retrieve parent folders of each file using getParents().
  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.

Sample script:

var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
  var file = files.next();
  var parents = file.getParents();
  while (parents.hasNext()) {
    var parent = parents.next();
    if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

      // do something

    }
  }
}

Pattern 2:

Flow:

  1. Retrieve folders from folder name of Project using getFoldersByName().
  2. Retrieve files in each folders using getFilesByName().
  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.

Sample script:

var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
  var folder = folders.next();
  var files = folder.getFilesByName(fileName);
  while (files.hasNext()) {
    var file = files.next();
    if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

      // do something

    }
  }
}

Pattern 3:

Flow:

  1. Retrieve folder IDs of folder name of Project using getFoldersByName().
  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().

Sample script:

var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = [];
while (folders.hasNext()) {
  folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
  var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
  var files = DriveApp.searchFiles(params);
  while (files.hasNext()) {
    var file = files.next();

      // do something

  }
});

References:

If these were not what you want, I'm sorry.

Upvotes: 1

Related Questions