繁体   English   中英

Google云端硬盘 - 如何列出指定文件夹中的所有文件

[英]Google Drive - How to List all Files in a specified folder

嗨我在这种情况下被困在这里,我已经尝试了许多不同的方法,但我无法得到我所追求的。

情况:我在google驱动器上托管的文件夹中有大量文件。 例如,报告的单个客户端文件夹有2000-10000个文件(这只会变大),我希望能够列出这些文件的标题和文件ID以及父文件夹ID,这样我就可以创建到文件的直接链接。 根据我的理解,我能够使用谷歌驱动器:drive-api-client-php我已经设置了一个项目并拥有客户端ID和客户端密码,我已经下载了subversion composer和google-api以及drive-api-client- php并使用XAMPP

到目前为止我做了什么:我已经通过谷歌文档和观看他们提供的教程视频。 我已经尝试了他们在网页上提供的谷歌样本 ,这是我需要的开始。 这将列出文件夹中包含ID和标题的文件或您选择的任何字段 - 这里的两个问题是您无法指定您想要的结果,最大结果数限制为1000.我在需要10,000。

我也看了一些谷歌的Apps脚本在这里 ,这似乎是使用像在Excel VBA中拉文件ID和标题等,但是这也仅限于结果的最大数量,然后超时问题。 这是我想要的确切类型的结果,但没有限制。

我的问题是:我有哪些选项可以在Google驱动器的指定文件夹中列出10,000个文件ID和标题,从开始到结束的过程是什么,因为我觉得我可能在早期错过了一个基本点,影响了我的结果可能性。 我对此很灵活,我可以在提取数据之后使用数据,只是首先获取数据对我来说是关键。 期待你的回复,非常感谢。 我希望这很清楚并且有道理。

在Apps脚本中尝试此代码:您需要编辑此代码,并替换您的文件夹ID。 在Google云端硬盘中打开一个文件夹,在浏览器地址栏中,您会看到:

https://drive.google.com/drive/folders/Your_Folder_ID_Here

复制出文件夹ID,并在下面的代码中替换它。 必须在Apps脚本和开发者控制台中的两个位置明确启用高级Google服务。

//This requires the Drive API To be turned on in the Advanced Google Services
function listFilesInFolder() {
  var query = 'trashed = false and ' +
      "'Your Folder ID Here' in parents";

  var filesInFolder, pageToken;

  do {
    filesInFolder = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken
    });
    if (filesInFolder.items && filesInFolder.items.length > 0) {
      for (var i = 0; i < filesInFolder.items.length; i++) {
        var thisFile = filesInFolder.items[i];
        //Logger.log('%s (ID: %s)', thisFile.title, thisFile.id);
        //To Do - Output content to file
        . . . . Code to output content . . . . 
      }
    } else {
      Logger.log('No files found.');
    }
    pageToken = filesInFolder.nextPageToken;
  } while (pageToken);
}

如果您正在寻找用PHP编写的代码,则需要使用不同的代码。

这就是我提出的,感谢上面的代码和我发现的其他一些代码的帮助,把它们放在一起,它做了我需要的。 它可能对其他人有用。

谢谢

function listFilesInFolder() {
var MAX_FILES = 2000; //use a safe value, don't be greedy
var id = 'FOLDER_ID_HERE'; 
var scriptProperties = PropertiesService.getScriptProperties();
var lastExecution = scriptProperties.getProperty('LAST_EXECUTION');
var sheet = SpreadsheetApp.getActiveSheet();
var data;
if( lastExecution === null )
lastExecution = '';

var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
  var iterator = continuationToken == null ?
  DriveApp.getFolderById(id).getFiles() : DriveApp.continueFileIterator(continuationToken);


 try { 
   for( var i = 0; i < MAX_FILES && iterator.hasNext(); ++i ) {
   var file = iterator.next();
   var dateCreated = formatDate(file.getDateCreated());
    if(dateCreated > lastExecution)
     processFile(file);
    data = [
       i,
       file.getName(),
       file.getId()
      ];        

    sheet.appendRow(data);
}
} catch(err) {
 Logger.log(err);
}

if( iterator.hasNext() ) {
 scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', iterator.getContinuationToken());
 } else { // Finished processing files so delete continuation token
 scriptProperties.deleteProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
 scriptProperties.setProperty('LAST_EXECUTION', formatDate(new Date()));
 }
 }

function formatDate(date) { return Utilities.formatDate(date, "GMT", "yyyy-MM-dd HH:mm:ss"); }

function processFile(file) {
var id = file.getId();
var name = file.getName();
//your processing...
Logger.log(name);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM