自动从n天之前的Google云端硬盘删除文件-“ getFolderById”问题

时间:2018-07-30 14:23:08

标签: google-apps-script google-drive-team-drive

我正在尝试删除特定Team Drive中(而不是整个Google Drive中)超过7天的文件和文件夹。

为实现这一点,我尝试合并在这里阅读的内容: Apps Script - Automatically Delete Files from Google Drive Older than 3 Days - Get List of Files

我没有足够的代表在此处添加评论,所以这就是我打开一个新线程的原因。在这里,您可以找到我从user1588938窃取的信息:

function getOldFileIDs() {
  var fileIDs = [];
  // Old date is 30 days
  var oldDate = new Date().getTime() - 3600*1000*24*30;
  var cutOffDate = Utilities.formatDate(new Date(oldDate), "GMT", "yyyy-MM-dd");

  // Get folderID using the URL on google drive
  var folder = DriveApp.getFolderById('XXXXXXX');
  var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '"');

  while (files.hasNext()) {
    var file = files.next();
    fileIDs.push(file.getId());
    Logger.log('ID: ' + file.getId() + ', Name: ' + file.getName());
  }
  return fileIDs;
};

function deleteFiles() {
  var fileIDs = getOldFileIDs();
  fileIDs.forEach(function(fileID) {
    DriveApp.getFileById(fileID).setTrashed(true);
  });
};

我坚持使用“ getFolderById”功能,因为我认为它不适用于Team Drive根目录,而仅适用于其中的文件夹。 确实,当我查看日志时,可以看到以下输出:

  var folder = DriveApp.getFolderById('this-is-my-team-drive-id');  

是通用名称: [18-07-30 06:34:49:146 PDT] Team Drive 而不是我选择的Team Drive的名称。

因此,我无法进一步处理该脚本。

关于如何使用 searchFiles 列出团队驱动器中每个文件和子文件夹的提示?

此解决方案可能适用,但它适用于Team Drive内的文件夹,而不适用于Team Drive的根目录: browse files in google team drive

谢谢!

2 个答案:

答案 0 :(得分:0)

如果有人试图达到相同的结果,这就是您的方法。

function deleteOldFiles() {
  var Folders = new Array(
    'YOUR-TEAM-DRIVE-ID' //you can find this in the team drive url
  );
  var DaysRetentionNumber = 15; //how many days old your files and folders must be before getting deleted?
  var RetentionPeriod = DaysRetentionNumber * 24 * 60 * 60 * 1000;

  Logger.clear();

  for each (var FolderID in Folders) {
    folder = DriveApp.getFolderById(FolderID);
    processFolder(folder);
  }

  function processFolder(folder){
    Logger.log('Folder: ' + folder.getName());
    var files = folder.getFiles();
    while (files.hasNext()) {
      var file = files.next();
      Logger.log('File: ' + file.getName());
      if (new Date() - file.getLastUpdated() > RetentionPeriod) {
        //file.setTrashed(true); //uncomment this line to put them in the trash
        //Drive.Files.remove(file.getId()); //uncomment this line to delete them immediately; CAREFUL!
        Logger.log('File '+ file.getName() + ' trashed');
      }
    }
    var subfolders = folder.getFolders();
    while (subfolders.hasNext()) {
      subfolder = subfolders.next();
      processFolder(subfolder);
    }
    checkEmptyFolder(folder);
  }

  function checkEmptyFolder(folder){
    if(!folder.getFiles().hasNext() && !folder.getFolders().hasNext()){
      Logger.log('Empty folder: '+ folder.getName());
      folder.setTrashed(true); // put them in the trash
    }
  }

  if(Logger.getLog() != '')
     MailApp.sendEmail('youremailaddresshere', 'Team Drive weekly cleanup report', Logger.getLog()); //get a log in your email so that you can see what will be deleted; try this before uncommenting the trash/delete lines!
}

答案 1 :(得分:0)

由于许多原因,

@Daniele INeDiA的脚本已损坏

  1. 脚本失败,并显示ReferenceError: "Drive" is not defined. (line 24, file "Code"),它指向//uncomment this line to delete them immediately;
  2. 如果根文件夹为空,脚本也会因Access denied: DriveApp. (line 39, file "Code")而失败。这是因为它无法区分根文件夹及其子文件夹。

我已经通过简单地将其注释掉而修复了#1,但这是#2的固定版本。

与此同时,我还为始终删除(始终打开/关闭始终删除)添加了一项功能。这样,您可以安排它每天晚上运行,以确保您从一个空文件夹开始新的一天。

P.S。 我确实看到了tehhowch的评论,即“不赞成使用每个(...中的...)”,因此随时添加替代方案,我将使用它。

function deleteOldFiles() {
  var Folders = new Array(
    'YOUR-TEAM-DRIVE-ID' //you can find this in the team drive's url
  );
  var DaysRetentionNumber = 15; //how many days old your files and folders must be before getting deleted?
  var RetentionPeriod = DaysRetentionNumber * 24 * 60 * 60 * 1000;
  var always_delete = true;

  Logger.clear();

  for each (var FolderID in Folders) {
    folder = DriveApp.getFolderById(FolderID);
    processFolder(folder, FolderID);
  }

  function processFolder(folder, FolderID){
    Logger.log('Folder: ' + folder.getName());
    var files = folder.getFiles();
    while (files.hasNext()) {
      var file = files.next();
      if (!always_delete)
        Logger.log('File: ' + file.getName());
      if (always_delete || new Date() - file.getLastUpdated() > RetentionPeriod) {
        file.setTrashed(true); //uncomment this line to put them in the trash
        // Don't uncomment the following because it breaks the script!
        //Drive.Files.remove(file.getId()); //uncomment this line to delete them immediately; CAREFUL!
        Logger.log('File '+ file.getName() + ' trashed');
      }
    }
    var subfolders = folder.getFolders();
    while (subfolders.hasNext()) {
      subfolder = subfolders.next();
      processFolder(subfolder);
    }
    if(Folders.indexOf(FolderID) == -1)
      checkEmptyFolder(folder);
  }

  function checkEmptyFolder(folder){
    if(!folder.getFiles().hasNext() && !folder.getFolders().hasNext()){
      Logger.log('Empty folder: '+ folder.getName());
      folder.setTrashed(true); // put them in the trash
    }
  }

  if(Logger.getLog() != '')
     MailApp.sendEmail('youremailaddresshere', 'Team Drive weekly cleanup report', Logger.getLog()); //get a log in your email so that you can see what will be deleted; try this before uncommenting the trash/delete lines!
}