Generating a file list from Google Drive?

BroodsSexton

Member
SoSH Member
Feb 4, 2006
12,630
guam
I would like to generate a list of filenames in a Google Drive directory. I've googled to look for instructions, but haven't found anything sufficiently accessible for me to be able to do this. Anyone have a good idea?
 

HampshireCounty

New Member
Aug 6, 2010
24
I do this with a Google Apps script. The following code works if the folder has less than a couple thousand files in it:

Code:
function listfolder(foldername) {
  var folders = DriveApp.getFoldersByName(foldername);
  var now = Date.now();
  var ssname = foldername + " " + now;

  if (folders.hasNext()) {
    var ss = SpreadsheetApp.create(ssname);
    ss.appendRow(["Filename", "URL"])
    var folder = folders.next();
    var files = folder.getFiles();
    while (files.hasNext()) {
      var file = files.next();
      ss.appendRow([file.getName(),file.getUrl()]);
    }
  } else {
    Logger.log("Could not find folder: ", foldername);
  }
}
Sorry, I should clarify - this code creates a Google Sheets document with the filename in the first column and the URL in the second column. That's not exactly what you asked for, but you could easily dump the second column, or create a Doc instead of a Sheet.
 

HampshireCounty

New Member
Aug 6, 2010
24
Open up the Google Drive web interface.

Go to New - More - Apps Script

Paste the above code in at the top, above the myFunction it automatically creates.

Change myFunction to something like this:
Code:
function myFunction() {
  listfolder("The Folder I Care About");
}
Save the script (you have to actually click on the floppy disk icon), and run it (the play button). It will ask for permission to access your Google Drive, then it will run for a minute. When it's done running, you should have a Google Sheet with the same name as the folder, plus a long string of numbers. That sheet should contain a list of all the files in that folder, and their URLs, if you keep that part of the code.

More on Apps Script at https://developers.google.com/apps-script/overview .