List all files and folder in google drive

前端 未结 2 1865
眼角桃花
眼角桃花 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: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

提交回复
热议问题