3

I am trying to get the list of files and folders from a particular folder in google drive using node.js. However I am only getting the list of folders and not the files within those folders. What am I doing wrong?

Here is my code.

app.get('/getFF', (req, res) => {
var folderid = <'my folder id'>;
var query = "'" + folderid + "' in parents";

drive.files.list({q: query, fields: 'files(id)'}, (err, resp) => {
    if (err) throw err;
    const files = resp.data.files;
    if (files.length) {
        files.map((file) => {
        console.log(file);
        });
        res.send(files);
    } else {
    resp.send('No files found');
    }
});

})

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • About `However I am only getting the list of folders and not the files within those folders.`, can I ask you whether my understanding is correct? In your situation, the folder of `folderid` has the subfolders and each subfolders also has the subfolders, and you want to retrieve the file list of all files from the subfolders. Is my understanding correct? If my understanding is correct, you want to also retrieve the folder structure? – Tanaike Mar 08 '21 at 01:46
  • Yes. The folder id is the folder I want to get the subfolders from with their list of files. Right now I am only able to see the subfolders but not their content. – Snugglepuss001 Mar 08 '21 at 01:54
  • Thank you for replying. From your replying, I proposed an answer. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike Mar 08 '21 at 03:14

1 Answers1

2

I believe your goal and your current situation as follows.

  • You want to retrieve the file list under the specific folder.
  • The specific folder has the subfolders.
  • You want to achieve this using googleapis for Node.js.
  • You have already been able to retrieve the file list using Drive API v3 with googleapis.

Modification points:

  • In the current stage, when drive.files.list({q: query, fields: 'files(id)'}) with var query = "'" + folderid + "' in parents" is used, the file list of the files and folders just under the folder of folderid are retrieved. It seems that this is the current specification. In order to retrieve the file list of all files under the specific folder which has the subfolders, it is required to retrieve the file list from each subfolder.
  • In this answer, I would like to propose to use a library of node-getfilelist for Node.js. This library can retrieve the file list of all files under the specific folder which has the subfolders. I created this library for such situation.

Usage:

1. Install library.

At first, please install the library as follows.

$ npm install --save-dev google-drive-getfilelist

or

$ npm install --global google-drive-getfilelist

2. Sample script.

const getfilelist = require("google-drive-getfilelist");

const topFolderId = "###"; // Please set the top folder ID.
getfilelist.GetFileList(
  {
    auth: auth,
    fields: "files(id)",
    id: topFolderId,
  },
  (err, res) => {
    if (err) {
      console.log(err);
      return;
    }
    const fileList = res.fileList.flatMap(({ files }) => files);
    console.log(fileList);
  }
);
  • In this case, auth is the auth of const drive = google.drive({version: 'v3', auth}).
Result:

When above script is run, the following result is obtained at console.log(fileList);. fileList is the file list of all files under the specific folder which has the subfolders.

[
  { id: '###' },
  { id: '###' },
  ,
  ,
  ,
]

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This actually works but there is one problem. While it lists the contents of the subfolders it does not list the subfolders so the output looks like the first level of the root folder. I wanted it to show the subfolders and the content within the subfolders. However thanks for sharing. It is much appreciated. – Snugglepuss001 Mar 08 '21 at 03:51
  • @Snugglepuss001 Thank you for replying. I apologize for the inconvenience. About the current output, I referred your script. I apologize for this. About `I wanted it to show the subfolders and the content within the subfolders.`, I couldn't notice this. I apologize again. In this case, this library returns the value of `res` including the folder structure and files under each subfolders. So how about checking the value of `res` using `console.log(JSON.stringify(res))` instead of `console.log(fileList)`? – Tanaike Mar 08 '21 at 04:52
  • @Snugglepuss001 If the value of `res` is not the result you expect, can you provide the sample output values you expect? By this, I would like to confirm it. Unfortunately, from your question, I cannot understand about the format of output you expect. I apologize for this. – Tanaike Mar 08 '21 at 04:57
  • Thank you so much. All of the data is shown when I used JSON.stringify(res). I can work at manipulating the data the way I want now. – Snugglepuss001 Mar 08 '21 at 06:52
  • @Snugglepuss001 Thank you for replying and testing it again. I'm glad your issue was resolved. Thank you, too. – Tanaike Mar 08 '21 at 08:08