I am trying to search through some files stored in a specific folder on Google Drive using Google Scripts. This is for creating a "Deployment Folder" for my school for the upcoming academic session.
Code below:
function start() { SearchFiles("TextToSearchFor");}function SearchFiles(query) { //put the source content folder ID and destination Drive folder ID here srcContentFolderID="srcID"; try{ var files = DriveApp.getFolderById(srcContentFolderID).searchFiles('(title contains "'+ query +'")'); while (files.hasNext()) { var file = files.next(); var fileName = file.getName(); var parents = file.getParents(); while (parents.hasNext()) { var parent = parents.next(); if(parent.getId() == srcContentFolderID){ //found the correct source of the file. Log it. Logger.log("Parent name: "+ parent.getName() +" File name:"+ fileName); } else Logger.log("NOT FOUND: "+ parent.getName() +" File name:"+ fileName); } } } catch(e){ Logger.log("ERROR: "+e.Message); }
I am using Apps Script runtime powered by Chrome V8.
I have the following question:
DriveApp.getFolderById(srcContentFolderID).searchFiles('(title contains "'+ query +'")')
doesn't work. I get the following exception:
Exception: Invalid argument: q at SearchFiles at Array.forEach GS_INTERNAL_top_function_call.gs
Only searching using DriveApp.searchFiles('(title contains "'+ query +'")')
does work, but it gives a lot of results.I am trying to do this for hundreds of files, so I risk timing out if I have to sift through multiple search results for every query.
I have looked through StackExchange for quite some time to figure out why an exception is generated, to no avail. Appreciate any help on this!
Note: I have seen the following links: