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() {

function SearchFiles(query) {

  //put the source content folder ID and destination Drive folder ID here

    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);
          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:





  • Welcome. Each question should be posted as separate questions. May 18, 2020 at 12:50
  • I tested your code, it work fine on my side. Please add more details in order to make other people to reproduce the error (how are you calling the function? what are you passing as parameter to SearchFiles? Are you usin the Chrome V8 runtime?) May 18, 2020 at 13:04
  • 1
    Thanks. Made changes to the post. Asked one question only. Yes, I am running the Chrome V8 runtime, and passing a search string to SearchFiles. Eg: SearchFiles("Worksheet"); May 18, 2020 at 13:22

1 Answer 1


The code works fine for me on a simple test. I suggest you to add more log statements but instead of using Logger use console. i.e. You could log every file name and their Mime Type in order to find if there is an specific file that is causing the error.

Also beside logging only e.message include the e.stack

console.error('%s, %s', e.message, e.stack)

NOTE: e.Message is wrong, it should be e.message

  • I made a mistake in reporting the original problem. Putting the search query as '(title contains "' + query + '")' doesn't go into the while loop at all. Putting in the search query as '(title contains "' + query + '") and (mimetype="application/pdf")' gives me the exception. Stack trace for that: ` Invalid argument: q, Exception: Invalid argument: q at SearchFiles1 (Code:428:18) at Code:195:17 at Array.forEach (<anonymous>) at Code:149:16 at Array.forEach (<anonymous>) at start (Code:142:12) at GS_INTERNAL_top_function_call.gs:1:8 ` May 18, 2020 at 14:00
  • @NikhilKarkare As I mentioned in my answer, the code in the question works fine for me. Rearding the new query, it looks that it has a type (it should be mimeType instad of mimetype) May 18, 2020 at 15:35
  • Thanks @rubén, I'm still getting the same exception. I'm wondering why the initial code works for you, and doesn't for me. Is it some permissions which I am missing? May 18, 2020 at 16:07
  • Maybe there is an issue with folder id or the folder. Try "unchaining" ` DriveApp.getFolderById(srcContentFolderID).searchFiles('(title contains "' + query + '")');` i.e. var folder = DriveApp.getFolderById(srcContentFolderID); (put here something to validate that you are really being able to get the folder) var files folder.searchFiles('(title contains "' + query + '")'); May 18, 2020 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.