如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件? [英] How to list all files in google drive in a folder with a set name?

查看:12
本文介绍了如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的驱动器中有一个文件夹,其中包含多个客户端"文件夹.每个客户文件夹具有相同的结构,其中包括一个包含已完成发票(称为已完成发票")的文件夹.

I have a folder in my drive which houses multiple "Client" folders. Each client folder has the same structure which includes a folder that contains completed invoices (called "Completed invoices").

我需要一种方法来遍历所有名为已完成发票"的文件夹,并在 google 表格中列出这些文件夹中的电子表格 ID,以便我以后可以遍历这些文件并从中提取数据.

I need a way to iterate through all the folders named "Completed Invoices" and list in a google sheet the spreadsheet Id's in those folders so I can loop through those files later and extract data from them.

我发现多个代码源有效,但仅在第一级 - 即它将列出代理/客户端文件夹中的文件,但不会进入子文件夹.例如.见下面的代码

Ive found multiple code sources that work but only on the first level - i.e. it will list the files in the agents/clients folder, but it will not go into the sub folders. E.g. see code below

function listFilesInFolder() {

   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);

    var folder = DriveApp.getFoldersByName("FOLDER ID GOES HERE");
    //Logger.log(folder);
    var contents = folder.getFiles();

    var cnt = 0;
    var file;

    while (contents.hasNext()) {
        var file = contents.next();
        cnt++;

        Logger.log(file);
        Logger.log(cnt);

            data = [
                file.getName(),
                file.getDateCreated(),
                file.getSize(),
                file.getUrl(),
,
            ];

            sheet.appendRow(data);



    };
};

推荐答案

感谢大家的回答,但经过更多搜索,我找到了答案.对于正在寻找类似解决方案的任何人 - 我找到了一个出色的脚本 here 它提供了一个非常强大的我想象的输出可以用于各种不同的场景:

Thanks everyone for your answers, but after some more searching I found my answer. For anyone who is looking for a similar solution - I found an excellent script here which provides a really robust output that I imagine could be used for a variety of different scenarios:

/** 
 * Google Apps Script - List all files & folders in a Google Drive folder, & write into a speadsheet.
 *    - Main function 1: List all folders
 *    - Main function 2: List all files & folders
 * 
 * Hint: Set your folder ID first! You may copy the folder ID from the browser's address field. 
 *       The folder ID is everything after the 'folders/' portion of the URL.
 * 
 * @version 1.0
 * @see     https://github.com/mesgarpour
 */

// TODO: Set folder ID
var folderId = 'My folder ID';

// Main function 1: List all folders, & write into the current sheet.
function listFolers(){
  getFolderTree(folderId, false);
};

// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true); 
};

// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // Get folder by id
    var parentFolder = DriveApp.getFolderById(folderId);

    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);

    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

  } catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();

  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    // Logger.log("Folder Name: " + childFolder.getName());
    data = [ 
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()
    ];
    // Write
    sheet.appendRow(data);

    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      // Logger.log("File Name: " + childFile.getName());
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()
      ];
      // Write
      sheet.appendRow(data);
    }

    // Recursive call of the subfolder
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  }
};

这篇关于如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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