Apps脚本 - 自动从Google云端硬盘中删除3天以上的文件 - 获取文件列表 [英] Apps Script - Automatically Delete Files from Google Drive Older than 3 Days - Get List of Files

查看:176
本文介绍了Apps脚本 - 自动从Google云端硬盘中删除3天以上的文件 - 获取文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的Google云端硬盘将我的IP摄像机的动态检测录像存储在家中。我正在运行以下脚本(稍微修改),以自动从Google云端硬盘中删除超过3天的文件:

I'm using my Google Drive to store motion detection recordings from my IP cameras at home. I'm running the following script (slightly modified) to automatically delete files older than 3 days from my Google Drive:

自动删除谷歌驱动器中的文件

脚本设置为运行凌晨5点左右。我遇到的问题是,当有太多的文件要删除时,我收到一个错误,说我已经超过了最大写入速率。这是因为set trashed命令包含在for循环中。如果我添加了一个睡眠来减慢循环,那么我得到另一个错误,表示脚本在允许的时间内没有完成。

The script is set to run every morning at around 5am. The problem I'm running into is, when there are too many files to delete, I get an error saying I've exceeded the max write rate. This is because the set trashed command is contained within a for-loop. If I add a sleep to slow down the loop, then I get another error saying the script didn't finish in the allowed time.

我可能只是运行它2或3次,但这是笨重的。

I could maybe just run it 2 or 3 times, but that's clunky.

如何修改此脚本以创建要删除的文件的临时列表,何时最终退出循环,执行删除?我没有太多GAS语言和语法的经验,所以具体细节真的很棒。

How can I modify this script to have it create a temporary list of the files to be deleted, and when it finally steps out of the loop, execute the delete? I don't have much experience with GAS language and syntax, so specifics would be really awesome.

推荐答案

此代码将返回所有超过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]);
  }
};

这篇关于Apps脚本 - 自动从Google云端硬盘中删除3天以上的文件 - 获取文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆