删除旧文件 [英] Delete old files

查看:169
本文介绍了删除旧文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助创建Google云端硬盘脚本,该脚本会自动删除除最近24个文件以外的所有旧文件。



我的网络驱动器每小时发送一次7z的数据库文件命名为:

  DATABASENAME_01.09.2017_07.bak.7z 



以及一般格式:

  DATABASENAME_DD。 MM.RRRR_HH.bak.7z 

Google云端硬盘上的文件直接被复制(我没有文件夹)。
我希望文件被删除而不是被移到垃圾箱中。这是关于释放空间。



我想雇佣其他解决方案在Stackoverflow上描述,但他们不符合我的要求。

据我所知,这个脚本不起作用,因为它使用了旧的Doclists函数。不幸的是,我不知道在script.google中使用的编程语言。我认为最好的方法是使用 DriveApp api。



您应该使用以下功能:


  • getFiles 允许您选择所有驱动器文件

  • 必须根据 getDateCreated()

  • 使用 removeFile api或者将最早的文件设置为与 setTrashed api一起被删除。您可以使用 Drive.Files.emptyTrash()清空垃圾(这是在高级Api中)。我们会看到这个方法并没有完全移除文件,并且会导致意想不到的结果。

  • 工作方式:必须通过< a href =https://developers.google.com/apps-script/guides/services/advanced =nofollow noreferrer>高级API Drive.Files (请仔细阅读如何在脚本中启用高级API)。这允许您为要删除的文件创建一个更快的查询(一个查询已经为我们组织了它们)



是或多或少我会做什么来解决你的问题:

  //获取所有文件
files_iterator = DriveApp.getFilesByType(application / 7z-compressed);

var file_list = [];
while(files_iterator.hasNext()){
var fl = files_iterator.next();
file_list.push(fl);


//对创建的日期文件进行排序
file_list = file_list.sort(function(fl1,fl2){
return fl1.getDateCreated() fl2.getDateCreated();
});

//删除不需要的文件
while(file_list.length> 24){
var fl = file_list.pop();
DriveApp.removeFile(fl);
}

此代码未经过测试,可能包含错误。

如果Mime Type不起作用,另一个解决方案是列出您云端硬盘中的所有文件,并只保留文件名以<$ c开头的文件$ c> DATABASENAME :

  var files_iterator = DriveApp.getFiles(); 

var file_list = [];
while(files_iterator.hasNext()){
var fl = files_iterator.next();
if(fl.getName()。match(DATABASENAME))
file_list.push(fl);





更稳定的版本

我写了一个看起来正常工作的新版本:

  function myFunction(){
//查询具有名称包含DATABASENAME作为前缀的文件的驱动器
//结果(项目)通过创建日期排序
var files = Drive.Files.list({
q:'标题包含DATABASENAME',
orderBy:'createdDate'
})。items;
//在保留最后24个元素的同时,为Array
files.slice(0,-25).forEach(function(file){$)的每个元素运行函数
// b $ b //删除文件(用其ID识别)
Drive.Files.remove(file.id)
});

如果您不启用高级API 即可。我已经测试了多次,只剩下最后24个元素。您必须等待该函数在再次运行之前结束其执行,否则数组可能变得不一致,第二次执行可能会删除不需要的文件。根据驱动器帐户中的文件数量,列表函数(作为 getFiles 函数)执行的查询统计限制。这种操作非常昂贵,每小时运行一次可能会使您超出配额。我建议你每天运行一次



关于 list 实际上它允许你通过 7z mimeType(application / x-7z-compressed
$ p $ Drive.Files.list({
q:'mimeType =application / x-7z - 压缩'
});



弃用带DriveApp的第一个版本



是我实际在我的AppScript和Drive上测试过的一个版本:

  function myFunction(){
var file_iterator = DriveApp.getFiles();

var file_list = [];
while(file_iterator.hasNext()){
var fl = file_iterator.next();
if(fl.getName()。match(DATABASENAMES))
file_list.push(fl);


//对创建的日期文件进行排序
file_list = file_list.sort(function(fl1,fl2){
return fl1.getDateCreated() fl2.getDateCreated();
});

//删除不需要的文件
while(file_list.length> 24){
var fl = file_list.pop();
// fl.setTrashed(true); //如果你想在垃圾箱
//而不是完全删除它。
DriveApp.removeFile(fl);
}
}

但这个版本的问题在于实际上不会从驱动器中删除文件。我不明白为什么这个函数实际存在,我没有看到它的用法。它仅从视图
中删除文件

I need help creating a Google Drive script that will automatically delete all old files except the 24 most recent files.

My network drive every hour sends to Google Drive packed 7z of a database file named:

DATABASENAME_01.09.2017_07.bak.7z

and in general the format:

DATABASENAME_DD.MM.RRRR_HH.bak.7z

The files on Google Drive are copied directly (I do not have any folders). I would like the files to be deleted instead of being moved to the trash. It's about releasing space.

I wanted to employ other solutions described on Stackoverflow, but they don't fulfill my requirements.

From what I've learned, the script does not work because it uses the old Doclists function. Unfortunately I do not know the programming language that is used in script.google

解决方案

I think that the best way is by using the DriveApp api.

There are some functions that you should use:

  • getFiles allows you to select all drive files
  • the FileIterator you receive must be sorted on the basis of getDateCreated()
  • remove the oldest file with the removeFile api OR set the oldest file as trashed with the setTrashed api. You can empty the trash with Drive.Files.emptyTrash() (this is in the Advanced Api). A we will see this method does not actually remove the file completely, and will lead to unexpected results.
  • Working approach: it is necessary to pass through the advanced API Drive.Files (Please read carefully how to enable the Advanced API in your script). This allows to create also a faster query for the files that you want to delete (a query that already organize them for us)

This is more or less what I would do to solve your problem:

// Get all the files
files_iterator = DriveApp.getFilesByType("application/7z-compressed");

var file_list = [];
while (files_iterator.hasNext()) {
  var fl = files_iterator.next();
  file_list.push(fl); 
}

// Sort the files on date created
file_list = file_list.sort(function(fl1, fl2) {
  return fl1.getDateCreated() < fl2.getDateCreated();
});

// Removing uneeded file
while (file_list.length > 24) {
  var fl = file_list.pop();
  DriveApp.removeFile(fl);
}

This code is not tested and probably contains error. Use it at your own risk.

Another solution if Mime Type does not work is to list all files in your Drive and keep only the one with filename starting with DATABASENAME:

var files_iterator = DriveApp.getFiles();

var file_list = [];
while (files_iterator.hasNext()) {
  var fl = files_iterator.next();
  if (fl.getName().match("DATABASENAME"))
    file_list.push(fl); 
}

More stable version

I've written a new version that seems to work correctly:

function myFunction() {
  // Query drive for the files that has name containing "DATABASENAME" as prefix
  // the result (items) is ordered by creating date
  var files = Drive.Files.list({
    q: 'title contains "DATABASENAME"', 
    orderBy: 'createdDate'
  }).items;
  // While keeping the last 24 elements, runs a function
  // for each element of the Array
  files.slice(0, -25).forEach(function(file) {
    // Deletes the file (recognized with its ID)
    Drive.Files.remove(file.id)
  });
}

This function does not work if you don't enable advanced API. I've tested multiple times and only the last 24 elements remains. You HAVE TO WAIT that the function ends its execution before running it again, or the Arrays may become inconsistent and the second execution may delete unwanted files. Depending on the number of files in your drive account, the list function (as the getFiles function) performs queries that are counted and limited. This operations are quite expensive and running it every hour may bring you to exceed the quota. I suggest you to run this function once a day.

A nice thing about list is that actually it allows you to search by means of string of the 7z mimeType ("application/x-7z-compressed"):

Drive.Files.list({
  q: 'mimeType = "application/x-7z-compressed"'
});

Deprecated First Version with DriveApp

This is a version that I have actually tested in my AppScript and on my Drive:

function myFunction() {
  var file_iterator = DriveApp.getFiles();

  var file_list = [];
  while (file_iterator.hasNext()) {
    var fl = file_iterator.next();
    if (fl.getName().match("DATABASENAMES"))
      file_list.push(fl); 
  }

  // Sort the files on date created
  file_list = file_list.sort(function(fl1, fl2) {
    return fl1.getDateCreated() < fl2.getDateCreated();
  });

  // Removing uneeded file
  while (file_list.length > 24) {
    var fl = file_list.pop();
    // fl.setTrashed(true); // if you want it in the trash
                            // instead of fully removed.
    DriveApp.removeFile(fl);
  }
}

but the problem with this version is that actually does not remove the file from the drive. I don't understand why this function exists actually, I don't see its usage. It only removes the files from the view

这篇关于删除旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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