如何在 Google Drive 中搜索子文件夹和子文件夹? [英] How do I search sub-folders and sub-sub-folders in Google Drive?

查看:25
本文介绍了如何在 Google Drive 中搜索子文件夹和子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个常见问题.

场景是:-

folderA____ folderA1____folderA1a
       \____folderA2____folderA2a
                    \___folderA2b

...问题是如何列出根目录下所有文件夹中的所有文件 folderA.

... and the question is how do I list all the files in all of the folders under the root folderA.

推荐答案

2020 年 4 月 Google 宣布从 2020 年 9 月起禁用多父文件.这改变了下面的叙述,意味着选项 2 不可用更长的选择.可以使用快捷方式来实现选项 2.当我测试新的限制/功能时,我会进一步更新这个答案<罢工>我们都习惯了 Windows/nix 等中的文件夹(又名目录)的概念.在现实世界中,文件夹是一个容器,其中放置了文档.也可以将较小的文件夹放在较大的文件夹中.因此,可以认为大文件夹包含其较小子文件夹中的所有文档.

April 2020 Google have announced that multi-parent files is being disabled from September 2020. This alters the narrative below and means option 2 is no longer an option. It might be possible to implement Option 2 using shortcuts. I will update this answer further as I test the new restrictions/features We are all used to the idea of folders (aka directories) in Windows/nix etc. In the real world, a folder is a container, into which documents are placed. It is also possible to place smaller folders inside bigger folders. Thus the big folder can be thought of as containing all of the documents inside its smaller children folders.

但是,在 Google Drive 中,文件夹不是容器,以至于在 Google Drive 的第一个版本中,它们甚至不被称为文件夹,而是被称为集合.文件夹只是一个具有 (a) 没有内容和 (b) 特殊 mime 类型 (application/vnd.google-apps.folder) 的文件.使用文件夹的方式与使用标签(又名标签)的方式完全相同相同.理解这一点的最好方法是考虑 GMail.如果您查看打开的邮件项目的顶部,您会看到两个图标.带有工具提示移至"的文件夹以及带有工具提示标签"的标签.单击其中任何一个,都会出现相同的对话框,并且都是关于标签的.您的标签在左侧列出,在一个看起来很像文件夹的树形显示中.重要的是,一个邮件项目可以有多个标签,或者您可以说,一个邮件项目可以位于多个文件夹中.Google Drive 文件夹的工作方式与 GMail 标签的工作方式完全相同.

However, in Google Drive, a Folder is NOT a container, so much so that in the first release of Google Drive, they weren't even called Folders, they were called Collections. A Folder is simply a File with (a) no contents, and (b) a special mime-type (application/vnd.google-apps.folder). The way Folders are used is exactly the same way that tags (aka labels) are used. The best way to understand this is to consider GMail. If you look at the top of an open mail item, you see two icons. A folder with the tooltip "Move to" and a label with the tooltip "Labels". Click on either of these and the same dialogue box appears and is all about labels. Your labels are listed down the left hand side, in a tree display that looks a lot like folders. Importantly, a mail item can have multiple labels, or you could say, a mail item can be in multiple folders. Google Drive's Folders work in exactly the same way that GMail labels work.

确定文件夹只是一个标签后,没有什么能阻止您在类似于文件夹树的层次结构中组织标签,事实上这是最常见的方式.

Having established that a Folder is simply a label, there is nothing stopping you from organising your labels in a hierarchy that resembles a folder tree, in fact this is the most common way of doing so.

现在应该清楚的是,文件夹 A2b 中的文件(我们称之为 MyFile)不是文件夹 A 的子或孙.它只是一个带有folderA2b"标签(混淆地称为父级)的文件.好的,那么我如何获取下"的所有文件?文件夹A?

It should now be clear that a file (let's call it MyFile) in folderA2b is NOT a child or grandchild of folderA. It is simply a file with a label (confusingly called a Parent) of "folderA2b". OK, so how DO I get all the files "under" folderA?

替代方案 1. 递归

诱惑是列出文件夹 A 的孩子,对于任何文件夹的孩子,递归地列出他们的孩子,冲洗,重复.在极少数情况下,这可能是最好的方法,但对于大多数情况,它存在以下问题:-

The temptation would be to list the children of folderA, for any children that are folders, recursively list their children, rinse, repeat. In a very small number of cases, this might be the best approach, but for most, it has the following problems:-

  • 为每个子文件夹执行一次服务器往返非常耗时.这当然取决于你的树的大小,所以如果你能保证你的树很小,那就没问题了.

备选方案 2. 共同父母

Alternative 2. The common parent

如果所有文件都由您的应用程序创建(即您正在使用 drive.file 范围),则此方法效果最佳.除了上面的文件夹层次结构,创建一个名为MyAppCommonParent"的虚拟父文件夹.当您将每个文件创建为其特定文件夹的子文件时,您也将其设为 MyAppCommonParent 的子文件.如果您记得将文件夹视为标签,这将变得更加直观.您现在可以通过简单地查询 MyAppCommonParent in parents 轻松检索所有后代.

This works best if all of the files are being created by your app (ie. you are using drive.file scope). As well as the folder hierarchy above, create a dummy parent folder called say "MyAppCommonParent". As you create each file as a child of its particular Folder, you also make it a child of MyAppCommonParent. This becomes a lot more intuitive if you remember to think of Folders as labels. You can now easily retrieve all descdendants by simply querying MyAppCommonParent in parents.

备选方案 3. 文件夹优先

首先获取所有文件夹.是的,所有这些.一旦您将它们全部存储在内存中,您就可以浏览它们的父属性并构建您的树结构和文件夹 ID 列表.然后,您可以在父母中执行单个 files.list?q='folderA' 或在父母中执行 'folderA1' 或在父母中执行 'folderA1a'....使用这种技术,您可以在两个 http 调用中获取所有内容.

Start by getting all folders. Yep, all of them. Once you have them all in memory, you can crawl through their parents properties and build your tree structure and list of Folder IDs. You can then do a single files.list?q='folderA' in parents or 'folderA1' in parents or 'folderA1a' in parents.... Using this technique you can get everything in two http calls.

选项3的伪代码有点像……

The pseudo code for option 3 is a bit like...

//从云端硬盘获取所有文件夹 files.list?q=mimetype=application/vnd.google-apps.folder 和垃圾箱=false&fields=parents,name//存储在地图中,以 ID 为键//找到文件夹 A 的条目并记下 ID//找到 ID 在父项中的任何条目,记下它们的 ID//对于每个这样的条目,递归重复//使用上面提到的所有 ID 来构造一个 ...//files.list?q='folderA-ID' in parents or 'folderA1-ID' in parents or 'folderA1a-ID' in parents...

备选方案 2 是最有效的,但只有在您可以控制文件创建的情况下才有效.备选方案 3 通常比备选方案 1 更有效,但在某些较小的树大小中 1 可能是最好的.

Alternative 2 is the most effificient, but only works if you have control of file creation. Alternative 3 is generally more efficient than Alternative 1, but there may be certain small tree sizes where 1 is best.

这篇关于如何在 Google Drive 中搜索子文件夹和子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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