在指定"AFolder"后如何递归读取子文件夹在父母那里? [英] How to recursivelsy read subfolders after speciying "AFolder" in parents?

查看:36
本文介绍了在指定"AFolder"后如何递归读取子文件夹在父母那里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,在Google Drive API中,我试图读取驱动器中的指定文件夹.

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

例如root/Temp/TestFiles/AreInHere

我想读取"AreInHere"中的所有文件,可以通过使用查询指定所需文件夹的ID来执行此操作.例如.'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’.

这可以正常工作并且按照ID的预期工作,它还返回我文件夹中的所有子目录,但是不会隐式地获取具有这些子目录的任何文件吗?

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?

我查看了API参考和所有用于搜索的文档,但没有找到任何OOTB的运气.

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.

预先感谢

推荐答案

不幸的是,使用Drive API无法自动列出子文件夹中包含的文件

相反,您需要使用一个递归函数来动态查找所有子文件夹和子文件夹的子文件夹-无论级别数是多少.

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 .

让父母的 id 动态地指定为查询参数 q ,并查询文件的 mimeType .

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.

您没有指定语言,例如在Apps脚本中,您可以执行以下操作:

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);
      }
    }
  }
}

这篇关于在指定"AFolder"后如何递归读取子文件夹在父母那里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆