遍历Google云端硬盘中的文件夹/子文件夹/文件 [英] Iterate Through Folders/Subfolders/Files in Google Drive

查看:86
本文介绍了遍历Google云端硬盘中的文件夹/子文件夹/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的Google云端硬盘结构设置:

I have a Google Drive structure setup like this:

Client A
--Project 1
----Drafts
----Presentations
----Minutes
--Project 2
----Drafts
----Presentations
----Minutes
Client B
<and so on>

我想编写一个脚本,在其中可以传递项目1的ID,它将遍历三个子文件夹中的每一个,并计算文件总数(最终,我想计算在一个文件夹中更新的文件数).日期范围,但要走一步!).

I want to write a script where I can pass in the ID of Project 1, and it will loop through each of the three subfolders and count the total number of files (ultimately I want to count the number of files updated over a date range, but baby steps!).

我尝试使用Class FolderIterator类代码,但是希望您将一组文件夹分配给变量文件夹"(显示为var folders = DriveApp.getFolders();,我尝试了var folders = DriveApp.getFolderById("<id of Project 1 here>");,但是在我的版本中,这只是一个文件夹,并且确实需要收集文件夹).如果我有父文件夹的ID,是否可以将这三个子文件夹分配给该folders变量?还是有更好的方法遍历子文件夹?

I've tried using the Class FolderIterator code, but that wants you to assign a group of folders to a variable "folders" (it showed var folders = DriveApp.getFolders(); and I tried var folders = DriveApp.getFolderById("<id of Project 1 here>"); but in my version that's just one folder, and it really needs the collection of folders). Is there a way to assign those three subfolders to that folders variable if I have the ID of the parent folder? Or is there a better way to loop through the subfolders?

推荐答案

DriveApp可能看起来像这样……

It may look some thing like this with DriveApp...

function traverseFolder(folder) {
  var name = folder.getName();

  if ( /^(Drafts|Presentations|Minutes)$/.test(name) ) { 
    var count = 0;
    var files = folder.getFiles();

    while ( files.hasNext() ) {
      count++; 
    }
    Logger.log(name + ' : ' + count);
  }
  var subs = folder.getFolders();

  while (subs.hasNext()) {
    traverseFolder(subs.next());
  }
}

我们现在也可以访问高级驱动器服务.也许我会看一个例子,如果有人不尽快发布一个例子.

We now have access to the Advanced Drive Service too. Maybe I'll look at an example with that if some one doesn't post one soon.

这篇关于遍历Google云端硬盘中的文件夹/子文件夹/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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