Neill
Neill

Reputation: 452

Search Google Drive for a file

I am trying to write a script to search my Google Drive for a file. The search should only look at the name of the file (not the contents), and look for a string within that name.

For example, there's a file called "2018-08-06_Miller_576132063_17.25.pdf" and I want to have my script search for "576132063" and get the fileID.

If it matters, I would be searching within subfolders of folder: "0B1-kfT4ZOAitb1lZSzM5YXR6czA"

function FileFinder() {

  var ShowMeCC = '576132063';
  var files = DriveApp.getFolderById('0B1-kfT4ZOAitb1lZSzM5YXR6czA').searchFiles('title contains  "' + ShowMeCC + '" ');
  while (files.hasNext()) {
    var file = files.next();
    var fnaMe = file.getName();
    var fID = file.getId();
    Logger.log(fID);
  }

}

The search finds nothing.

This seems basic, yet I can't find anyone who asked this specific question.

Thanks in advance!

Upvotes: 1

Views: 257

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to retrieve files which have the filename including the string of ShowMeCC.

If my understanding is correct, how about this workaround?

The official document says as follows.

The contains operator only performs prefix matching for a title. For example, the title "HelloWorld" would match for title contains 'Hello' but not title contains 'World'.

By this, unfortunately, the file of 2018-08-06_Miller_576132063_17.25.pdf cannot be directly retrieved using title contains '576132063'. So as a workaround, it is considered the following workaround.

  • Search the file after all files were retrieved.

In this case, at first, it is required to retrieve all files. But this is the high cost. In order to reduce the cost, I would like to propose 2 step searching.

  1. Retrieve files using the query of fullText contains '576132063'.
    • fullText contains '576132063' can search the filename like 2018-08-06_Miller_576132063_17.25.pdf.
  2. Retrieve the file from files retrieved by fullText contains '576132063'.

By this flow, all files are not required to be retrieved. So the cost becomes lower than that of above method.

Modified script 1:

function FileFinder() {
  var ShowMeCC = '576132063';
  var files = DriveApp.searchFiles('fullText contains "' + ShowMeCC + '"'); // Modified
  // OR var files = DriveApp.getFolderById('0B1-kfT4ZOAitb1lZSzM5YXR6czA').searchFiles('fullText contains "' + ShowMeCC + '"'); // Modified
  while (files.hasNext()) {
    var file = files.next();
    var fnaMe = file.getName();
    if (fnaMe.indexOf(ShowMeCC) > -1) { // Added
      var fID = file.getId();
      Logger.log(fnaMe);
      Logger.log(fID);
    }
  }
}

Reference:

If this modification was not the result you want, I apologize.

Added:

As one more sample, if you want to retrieve the file from a specific folder like DriveApp.getFolderById('0B1-kfT4ZOAitb1lZSzM5YXR6czA'), you can also use the following script. When the number of files in the folder is not much, the cost of this method will not become high.

Modified script 2:

function FileFinderaaa() {
  var ShowMeCC = '576132063';
  var files = DriveApp.getFolderById('0B1-kfT4ZOAitb1lZSzM5YXR6czA').getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var fnaMe = file.getName();
    if (fnaMe.indexOf(ShowMeCC) > -1) {
      var fID = file.getId();
      Logger.log(fnaMe);
      Logger.log(fID);
    }
  }
}

Upvotes: 2

Related Questions