0

我正在尝试删除特定团队云端硬盘(而不是整个 Google 云端硬盘)中超过 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 列出团队云端硬盘中的每个文件和子文件夹的任何提示?

此解决方案可能适用,但它适用于团队云端硬盘中的文件夹,而不是团队云端硬盘的根目录: browse files in google team drive

谢谢!

4

2 回答 2

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!
}
于 2018-10-22T08:55:26.017 回答
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 的固定版本。

同时,我还添加了一个始终删除的功能(always_delete on/off)。这样,您可以安排它每晚运行,以确保您从一个空文件夹开始新的一天。
同时,我还添加了一个开/关开关以通过电子邮件发送日志。

PS 我确实看到了 tehhowch 的评论“对于每个 (... in ...) 已弃用”,所以请随意添加一个替代方案,我将使用它。

function deleteOldFiles() {
  var sendlog = false, 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;
  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(sendlog && Logger.getLog() != '')
     MailApp.sendEmail('youremailaddresshere', 'Team Drive 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!
}
于 2019-01-11T13:03:30.160 回答