且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apps脚本 - 自动从Google云端硬盘中删除3天以上的文件 - 获取文件列表

更新时间:2023-12-04 20:57:58

此代码将返回所有超过30天的文件的文件ID。它与另一篇文章中引用的代码非常不同。您使用的代码中已经有一个不推荐的类。 DocsList 现已弃用。我给你的代码使用 DriveApp ,并使用 searchFiles()方法。

This code will return all the file ID's of files that are older than 30 days old. It is very different than the code referenced in the other post. The code that you are using has a deprecated class in it. DocsList is now deprecated. The code I'm giving you uses DriveApp, and makes use of the searchFiles() method.

Google文档 - 搜索文件

function getFilesByDate() {
  var arrayOfFileIDs = [];

  var ThirtyDaysBeforeNow = new Date().getTime()-3600*1000*24*30;
    // 30 is the number of days 
    //(3600 seconds = 1 hour, 1000 milliseconds = 1 second, 24 hours = 1 day and 30 days is the duration you wanted
    //needed in yr-month-day format

  var cutOffDate = new Date(ThirtyDaysBeforeNow);
  var cutOffDateAsString = Utilities.formatDate(cutOffDate, "GMT", "yyyy-MM-dd");
  //Logger.log(cutOffDateAsString);

  var theFileID = "";

  //Create an array of file ID's by date criteria
  var files = DriveApp.searchFiles(
     'modifiedDate < "' + cutOffDateAsString + '"');

  while (files.hasNext()) {
    var file = files.next();
    theFileID = file.getId();

    arrayOfFileIDs.push(theFileID);
    //Logger.log('theFileID: ' + theFileID);
    //Logger.log('date last updated: ' + file.getLastUpdated());
  }

  return arrayOfFileIDs;
  //Logger.log('arrayOfFileIDs: ' + arrayOfFileIDs);
};

您可以取消评论 Logger.log()语句,运行代码,看看代码如何返回。有一个 Logger.log()语句:

You can un-comment the Logger.log() statements, run the code, and see what the code returns if you want. There is one Logger.log() statement:

//Logger.log('date last updated: ' + file.getLastUpdated());

这将打印检索的文件的日期。所以您可以在删除任何文件之前查看。

That will print the dates of the files that were retrieved. So you can look at that before you delete any files.

该代码不会删除任何文件,或设置任何要删除的文件。它只是按日期搜索文件,并创建一个所有文件ID的数组。

That code doesn't delete any files, or set anything to trashed. It just searches files by date, and creates an array of all the file IDs.

所以删除或删除文件的功能可以先调用该函数并获取列表的所有文件ID的工作。

So the function to delete or trash the files could first call that function and get a list of all the file ID's to work on.

如果您要删除您的文件,而不先将其设置为已删除,那么可以完成。

If you want to delete your files without first setting them to trashed, that can be done.

您需要在开发者控制台中的Apps脚本项目 AND 中明确启用高级Google服务。如果你不这样做,这将不起作用。

You need to explicitly enable the Advanced Google Services in your Apps Script project AND in the developers console. If you don't do both, it won't work.

//This requires the Drive API To be turned on in the Advanced Google Services
//Under the RESOURCES menu, choose ADVANCED GOOGLE SERVICES
function deleteFile(idToDLET) {
  idToDLET = 'the File ID';

  //This deletes a file without needing to move it to the trash
  var rtrnFromDLET = Drive.Files.remove(idToDLET);
}

要调用获取文件ID以删除的功能,它将看起来像这个:

To call the function that gets the file ID's to delete, it would look like this:

function deleteFiles() {
  var arrayIDs = getFilesByDate();

  for (var i=0; i < arrayIDs.length; i++) {
    Logger.log('arrayIDs[i]: ' + arrayIDs[i]);
    //This deletes a file without needing to move it to the trash
    var rtrnFromDLET = Drive.Files.remove(arrayIDs[i]);
  }
};