Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all files and folder in google drive

I've been trying to figure this out for a while now. I hope I can get some guidance on this. The purpose of the following script is to get a full list of folders and files with subfolders and their files included.

Here is what I currently have:

  var counter = 0

  var files = folder.getFiles();  
  var subfolders = folder.getFolders();
  var folderPath = folder.getName();

  while (subfolders.hasNext()){   
    subfolder = subfolders.next(); 
    var row = [];
    //row.push(subfolder.getName(),'',subfolder.getId(),subfolder.getUrl(),subfolder.getSize(),subfolder.getDateCreated(),subfolder.getLastUpdated());
    //list.push(row); 
    if(counter > 0){
      var files = subfolder.getFiles();
    }

    while (files.hasNext()){
      file = files.next();
      var vals = file.getUrl();
      var row = [];
      if(counter == 0){
        row.push(folder.getName(),file.getName(),file.getId(),file.getUrl(),file.getSize(),file.getDateCreated(),file.getLastUpdated())
      }else{
        row.push(folderPath + '/' + subfolder.getName(),file.getName(),file.getId(),file.getUrl(),file.getSize(),file.getDateCreated(),file.getLastUpdated())        
      }
      list.push(row);    
    }
    counter = counter + 1
  }

It currently gets the folder names and file names for the current folder and it's subfolder. It doesn't go any further than that. I'm stuck trying to figure out how to get a loop going to continue until there are no more sub-folders.

It isn't a very big drive. There are less than 10 levels but would like the flexibility to go further if needed.

like image 341
DanCue Avatar asked Feb 27 '19 18:02

DanCue


People also ask

How do I get a list of files and folders in Google Drive?

Call the listFiles method with the Drive folder name and it will create a list of all files and appends them to a spreadsheet.

How do I get a list of files in a Drive?

For Windows 10, follow these instructions: Hold the windows key and press "r," type in "cmd" and then press enter, type in "cd ../.." and then press enter, type in "tree" and then press enter. This will usually show all of the files on your hard drive.

Can you export a list of files from Google Drive?

You can export and download your data from Google Drive, which includes items from Google Docs, Sheets, Slides, Drawings, Sites, Drive, Forms, and Jamboard. You can create an archive to keep for your records or use the data in another service. You can download files that haven't been deleted.


1 Answers

Recursion is beneficial in this case. The code below calls the recursive method recurseFolder() which takes a Folder and Array as a parameter. It adds all the files in the folder to a list, then calls itself on any subfolders it finds.

function test(){
  var root = DriveApp.getRootFolder();
  var list = [];

  var list = recurseFolder(root, list);
  Logger.log(JSON.stringify(list));

  //This is just how I am testing the outputed list. You can do what you need.
  var sheet = SpreadsheetApp.getActiveSheet();
  list.forEach(function (row){
   sheet.appendRow(row); 
  });
}

function recurseFolder(folder, list){
  var files = folder.getFiles();  
  var subfolders = folder.getFolders();

  while (files.hasNext()){ //add all the files to our list first.
    var file = files.next();
    var row = [];
    Logger.log("File: " + folder.getName());
    row.push(folder.getName(),file.getName(),file.getId(),file.getUrl(),file.getSize(),file.getDateCreated(),file.getLastUpdated())
    list.push(row);
  }


  while (subfolders.hasNext()){   //Recurse through child folders.
    subfolder = subfolders.next(); 
    Logger.log("Folder: " + subfolder.getName());
    list = recurseFolder(subfolder, list); //Past the original list in so it stays a 2D Array suitible for inserting into a range.
  }

  return list;
}

I'm not sure if the output is formatted how you intended so you might need to play with it a little. Note: It will easily time out if run on a larger Drive.

like image 111
Chris Avatar answered Oct 13 '22 20:10

Chris