In Google Drive I'm trying to search for a file in the current directory. The script is linked to a spreadsheet, so it finds the spreadsheet's parent directory, then uses its id to specify a search within the folder for a file named Title.
The code I have so far is:
function searchFolder(){
//get current folder id
var ss = SpreadsheetApp.getActive(); //current spreadsheet
var directParents = DriveApp.getFileById(ss.getId()).getParents();
while( directParents.hasNext() ) {
var folder = directParents.next();
var folderId = folder.getId();
Logger.log(folder.getName() + " has id " + folderId);
}
//get files in folder
var parent = DriveApp.getFolderById(folderId); // folder Id
var search = 'name contains "Title"';
var specificFolder = parent.searchFiles(search);
while (specificFolder.hasNext()) {
var file = specificFolder.next();
Logger.log(file.getName());
Logger.log(file.getId());
}
}
I get the error "Invalid argument: query" at the beginning of the while loop, which suggests that no files were found.
This error seems to only occur when I search by name. If I change the search variable to 'fullText contains "hello"' (the file Title has 'hello' in it) then there are no problems. What can be done to make the search by name work?
To replicate the error, make two spreadsheets in a folder on google drive. One spreadsheet has the script linked to it, the other is named "Title" and is being searched. Thanks.