How to recursivelsy read subfolders after speciying “AFolder” in parents?

白昼怎懂夜的黑 提交于 2021-01-29 10:11:47

问题


Basically, in Google Drive API, I’m trying to read a specified folder within the drive.

E.g. root/Temp/TestFiles/AreInHere

I want to read all files in ‘AreInHere’, and I can do this using a query specifying the id for my desired folder. E.g. ‘’’AreInHere’ in parents’.

This works fine and does as id expect, it also returns all the sub-directories in my folder but doesn’t reclusively get any files with those sub-directories?

I have looked at the API reference and all the documentation for searching but have has no luck in finding anything OOTB.

Any help or advice is much appreciated.

Thanks in advance


回答1:


Unfortunately, with Drive API there is no way to automatically list files contained within child folders

Instead, you need to use a recursive function that dynamically finds all subfolders and subfolders of subfolders - no matter what the number of levels is .

What helps to make your life easier is specifying the id of the parent dynamically as a query parameter q and querying for the mimeType of the file.

You don't specify your language, but e.g. in Apps Script you could do something like this:

function myFunction() {
  var id = "SPECIFY HERE THE ID OF THE PARENT FOLDER";
  iterate(id);

}

function iterate(id){
  var q = "'" + id + "' in parents"
  var files= Drive.Files.list({"q": q}).items;
  if (files.length>0){
    for ( var i = 0; i < files.length; i++){
      Logger.log(files[i].title);
      Logger.log(files[i].mimeType);
      if(files[i].mimeType=="application/vnd.google-apps.folder"){
        id = files[i].id;
        iterate(id);
      }
    }
  }
}


来源:https://stackoverflow.com/questions/59964316/how-to-recursivelsy-read-subfolders-after-speciying-afolder-in-parents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!