How can i list all files in google drive folder?

I've a folder i have listed all the files i need for my application .
I wanted to know is there any way or any extension through which i can list all the files in my google drive?
Thankyou in advance :slight_smile:

google apps script:

// return list of files in a folder
function getListOfFilesInFolder(targetID) {
  filesList="";
  var parentFolder = DriveApp.getFolderById(targetID);
  var files = parentFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    filesList += file.getName()+ "|" + file.getId() + ",";
  }
  var output = filesList.replace(/,\s*$/, "");  //removes last comma
  return ContentService.createTextOutput( output );
}
1 Like

But i dont know how and where should i use that.

@TIMAI2 can you help me ?

You can set this up in a script bound to a google sheet, and generate a list of the files to be placed in a set of columns and rows in that google sheet.

You can then download this list from the google sheet to the app

If you want to automate a bit more, you can create a google apps script web app, which will do all that for you.

Okay, Thankyou to you.

Note, the script is designed for a web app. To use as a bound script to the google sheet, it will require reworking to set the data to the sheet.

I will use the guide and will ask you if i got any problem .
Also i found somebody made an extension,

It is your money....

I'll be trying to develop the extension.

Something i found :

Also
https://developers.google.com/drive/api/v2/reference/files/list

It should really only take about five minutes to setup the google web app, then you are done.

No need for extensions....

HOWTO: Create a Google Apps Script Web App bound to a Spreadsheet

This way you can keep your sheet private and your wallet full :slight_smile:

1 Like

I;ll 100% give try tomorrow. Thankyou for the guide @TIMAI2 :heart: :heavy_heart_exclamation:

Use this script in your web app:

// return list of files in a folder
function doGet(e) {
  filesList=[];
  var parentFolder = DriveApp.getFolderById(e.parameter.folderID);
  var files = parentFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    filesList.push([file.getName(),file.getId()]);
  }
  var output = JSON.stringify(filesList);
  return ContentService.createTextOutput( output );
}

It will return a JSON stringified list of lists, like this:

[["mytextfile.txt","1EBp2WvxLcbmVKsmNbI2WhZEtYg0"],["Technical Archive","1URGJhNETB3Er864X2enE_EHmcD"]]

I did something like that:

Then i deployed it

Result :
Success: undefined, undefined added

It is not listing files ,
Am i doing some mistakes ?
@TIMAI2

Also i have not used folder id that is connected with my google account, The folder id is another's public folder id. Is it okay or i have to do with another's google account.??

You don't have this written correctly:

Either:

var parentFolder = DriveApp.getFolderById('DKVOPRYIKPDOVNSEPOSKVLGKK');

or

var parentFolder = DriveApp.getFolderById(e.parameter.folderID);

and send the folderID parameter via the scriptURL:

https://<scriptURL>?folderID=DKVOPRYIKPDOVNSEPOSKVLGKK

Somebody suggested me to use this code:
function myFunction123() {
var foldername = 'Folder Name';
var folderlisting = 'listing of folder ' + foldername;

  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();
  var contents = folder.getFiles();
  
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow(['name','link']);
  
  var file;
  var name;
  var link;
  var row; 
  while(contents.hasNext()){
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    sheet.appendRow([name, link]);
  }
  
};

So i used that and its perfectly working for me...

Is it possible using the script you have provided? @TIMAI2

Why not ask the person who's script you ARE using?

My script returns the data directly to your app, not to a spreadsheet...

Sorry :sweat: :sweat:


Here is what i tried but after deploy it says : Script function not found: doGet

https://script.google.com/macros/s/AKfycbwcb6yLlN0eaE9w7WNC0YsoKeCu3Oga-p_vRa2zIUub74xvw19uuTpfeRgzzFssGzSe/exec

I gave you the answer above.

Do not run the doget(e) function from the script editor, it will not work.

Run it from the app or in a browser using the script url (and parameters if you use them)

Yes it worked. Thankyou @TIMAI2 . Very much thankyou to you :heavy_heart_exclamation: