How to search for files by name inside of a specific folder Google Docs API

Viewed 1159

I am using V3 of the google docs api and trying to figure out how to search for a file by name inside a specified folder.

This is what I currently have:

function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});
  drive.files.list({
    q: 'name = "NAMEHERE" and parents in "FOLDERID"',
    pageSize: 10,
    fields: 'nextPageToken, files(id, name, webViewLink)',
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const files = res.data.files;
    if (files.length) {
      console.log('Files:');
      files.map((file) => {
        console.log(`${file.name} ${file.webViewLink}`);
      });
    } else {
      console.log('No files found.');
    }
  });
}
1 Answers

I believe your goal as follows.

  • You want to search a file in the specific folder on Google Drive using the filename.
  • You want to achieve this using googleapis with Node.js.
  • In your script, auth can be used for using the method of files.list in Drive API.

For this, how about this answer?

I could confirm that you are using Drive API v3. So how about the following modification?

From:

q: 'name = "NAMEHERE" and parents in "FOLDERID"',

To:

q: 'name = "NAMEHERE" and "FOLDERID" in parents',

or

q: "name = 'NAMEHERE' and 'FOLDERID' in parents",
  • Please use in parents like 'FOLDERID' in parents.
    • At above modified query, the file with the filename of NAMEHERE in the folder of FOLDERID is retrieved. In this case, when there are several files with the same filename in the folder, those are retrieved.
  • When above modification is reflected to your script of listFiles, I could confirm that the script worked. In this case, it supposes that the authorization can be done. If you want to know the script for the authorization, please check Node.js Quickstart.

References:

Related