0

我正在尝试删除特定团队云端硬盘(而不是整个 Google 云端硬盘)中超过 10 天的文件和文件夹。

为了做到这一点,我试图合并我在这里读到的内容

我试图在Google app script上运行以下脚本。运行脚本时出现此错误

Syntax error: SyntaxError: Unexpected identifier line: 10 file: Code.gs

我坚持使用“getFolderById”函数。这就是第 10 行中的内容。


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!
}

如何修改此脚本以删除所有超过 10 天的团队驱动器文件?

我也试过这个。但对 Google 团队驱动器没有任何作用

4

1 回答 1

2
function delFilesInFolderOlderThanXday() {
  const folder=DriveApp.getFolderById('id');
  const files=folder.getFiles();
  const dt=new Date();
  const threshold=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-X).valueOf();
  while(files.hasNext()) {
    let file=files.next();
    let dtv=new Date(file.getLastUpdated()).valueOf();
    if(dtv<threshold) {
      Drive.Files.remove(file.getId(),{supportsAllDrives:true});
    }  
  }
}

如果您想删除,您必须启用 Drive API。否则,您可以使用 DriveApp 将它们放入垃圾箱。您还必须设置 X。

我在 2022 年 2 月 4 日测试了这段代码,它仍在工作

实际上要清楚我测试了这段代码,因为我只是想确保 Drive.Files.remove() 仍然有效:

function delFilesInFolderOlderThanXday() {
  const folder = DriveApp.getFolderById('11wTZxbF9r-WlMK1uJlbcbmol7UcDV2X1');
  const files = folder.getFiles();
  const dt = new Date();
  while (files.hasNext()) {
    let file = files.next();
    Drive.Files.remove(file.getId(), { supportsAllDrives: true });
  }
}
于 2021-04-04T18:09:06.253 回答