5
votes

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.

2
For example, is this library useful for your situation? github.com/tanaikech/FilesAppTanaike

2 Answers

9
votes

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.

0
votes

You need a function that will navigate the folder structure recursively, meaning that if it runs into a subfolder within a folder, it will call itself again passing that folder as a new parent.

function listFolders(parentFolderId) {

var sourceFolder = DriveApp.getFolderById(parentFolderId) || DriveApp.getRootFolder();

  var folders = sourceFolder.getFolders();

  var files = sourceFolder.getFiles();

  while (files.hasNext()) {

    var file = files.next();              
    //Do something
  }

  while (folders.hasNext()) {
    var folder = folders.next();         
    listFolders(folder.getId());

  }

}

Note that this function will still time out if you have lots of files in your Drive, in which case you need to store the state of your app using PropertiesService and schedule the function to run again using triggers via the ScriptApp. You can achieve this by saving the continuation token for your Files Iterator between script executions More on ContinuationToken