自动从Google共享驱动器删除X天以上的文件



我正在尝试删除特定Team Drive中(而不是整个Google Drive中(超过10天的文件和文件夹。

为了实现这一点,我试图合并我在这里读到的

我尝试在谷歌应用程序脚本上运行以下脚本。在运行脚本时,我得到了这个错误

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天的团队驱动器文件?

我也试过这个。但谷歌团队驱动没有任何效果

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});
}  
}
}

如果要删除,则必须启用驱动器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 });
}
}

相关内容

  • 没有找到相关文章

最新更新