如何在Drive API for Javascript中一次删除多个文件?

时间:2017-03-10 15:44:02

标签: javascript google-api google-drive-api google-drive-realtime-api google-apis-explorer

function deleteFiles(fileId,supportsTeamDrives) {



var date = new Date();
date.setDate(date.getDate() - 180);
var n = date.toISOString().split('.')[0] ;
  var test = false;

  gapi.client.drive.files.list({
    pageSize: x,
     q: "starred = "+test+" and viewedByMeTime < '"+n+"'",
     orderBy: 'quotaBytesUsed desc',

      fields: "nextPageToken, files(id, name, viewedByMeTime, mimeType, quotaBytesUsed)",

     }

    )

  .then(function(response) {
  var files = response.result.files;

      if (files && files.length > 0) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          var file_id = file.id,
          )');

           }}

  var request = gapi.client.drive.files.delete({
  supportsTeamDrives: 'false',
 fileId: file_id ,
  });   }

 request.execute(function(resp) { });




}

我希望此函数列出fileID,因为每个fileID都被删除,它们会被删除。如何将列表文件和删除文件功能组合在一起才能生效?

目前我将它们分成两个函数,但变量fileID只保存输出的最后一个fileID,因此,delete函数只删除一个文件,列出最后一个文件。我希望它列出并将每个文件输出到varable,并且如其列出的它将被删除。我可以改变或添加什么来实现这一目标?

1 个答案:

答案 0 :(得分:1)

您可以更改代码以删除循环中的文件:

    function deleteFiles(fileId, supportsTeamDrives) {

    var date = new Date();
    date.setDate(date.getDate() - 180);
    var n = date.toISOString().split('.')[0];
    var test = false;

    gapi.client.drive.files.list({
                pageSize: x,
                q: "starred = " + test + " and viewedByMeTime < '" + n + "'",
                orderBy: 'quotaBytesUsed desc',

                fields: "nextPageToken, files(id, name, viewedByMeTime, mimeType, quotaBytesUsed)",

            }

        )

        .then(function(response) {
            var files = response.result.files;

            if (files && files.length > 0) {
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    var file_id = file.id;
                    deleteFile(file_id);
                }
            }

        });
}

// method used to delete the files
function deleteFile(file_id) {
    var request = gapi.client.drive.files.delete({
        supportsTeamDrives: 'false',
        fileId: file_id,
    });
    request.execute(function(resp) {});
}