2

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:

https://stackoverflow.com/questions/35661979/searching-for-file-by-name-within-a-folder-in-google-drive-using-google-scripts

https://stackoverflow.com/questions/52100052/folder-searchfiles-method-raises-invalid-argument-q-error/52100980

https://developers.google.com/drive/api/v2/ref-search-terms

https://stackoverflow.com/questions/56596130/google-app-script-driveapp-getparents-not-working-as-expected

3
  • Welcome. Each question should be posted as separate questions. – Rubén May 18 '20 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?) – Rubén May 18 '20 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"); – Nikhil Karkare May 18 '20 at 13:22
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

6
  • 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 ` – Nikhil Karkare May 18 '20 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) – Rubén May 18 '20 at 15:35
  • *typo ^^^^ ref. developers.google.com/drive/api/v3/search-files – Rubén May 18 '20 at 15:41
  • 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? – Nikhil Karkare May 18 '20 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 + '")'); – Rubén May 18 '20 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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