2

I am trying to print all folder names from the parent folder in google drive

I tried so many methods but not working..

const drive = google.drive({ version: 'v3', auth });
console.log("Drive => ", drive.drives.appdata);
drive.files.list({
    //spaces: 'drive',
    //isRoot: true,
     //pageSize: 10,
}, (err, res) => {
    console.log(err);
    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);
        });
    } else {
        console.log('No files found.');
    }
});

Above code only giving the file name of those folder but I am trying to print all parent folder names.?

I need to print angular_files, datastructurealgo,DSA,Tools names. These all are folders.

enter image description here

mkHun
  • 5,891
  • 8
  • 38
  • 85

1 Answers1

1

You can use this code from the official documentation to search for folders in Google Drive. The key here is to use q: "mimeType='application/vnd.google-apps.folder'" as parameter of drive.files.list

Sample Code:

const drive = google.drive({ version: 'v3', auth });
console.log("Drive => ", drive.drives.appdata);
drive.files.list({
    q: "mimeType='application/vnd.google-apps.folder'",
    fields: 'nextPageToken, files(id, name)',
    spaces: 'drive',
    pageToken: pageToken
}, (err, res) => {
    console.log(err);
    if (err) return console.log('The API returned an error: ' + err);
    if (res.files.length) {
      res.files.forEach(function (folder) {
        console.log('Found folder: ', folder.name, folder.id);
      });
      pageToken = res.nextPageToken;
    } else {
        console.log('No folders found.');
    }
});

Please note that you might need to tweak the code based on your need.

CMB
  • 4,950
  • 1
  • 4
  • 16