List all files and folder in google drive

前端 未结 2 1864
眼角桃花
眼角桃花 2021-01-20 22:57

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

相关标签:
2条回答
  • 2021-01-20 23:26

    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 讨论(0)
  • 2021-01-20 23:29

    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

    0 讨论(0)
提交回复
热议问题