Reputation: 452
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
Reputation: 201378
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.
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.
fullText contains '576132063'
.
fullText contains '576132063'
can search the filename like 2018-08-06_Miller_576132063_17.25.pdf
.fullText contains '576132063'
.By this flow, all files are not required to be retrieved. So the cost becomes lower than that of above method.
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);
}
}
}
If this modification was not the result you want, I apologize.
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.
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