Google apps script 谷歌应用程序脚本删除所有文件从谷歌驱动器?
我需要从我的驱动器中删除超过16 GB的所有文件,我需要手动删除几个小时 寻求支持谷歌的帮助,但没有得到任何帮助Google apps script 谷歌应用程序脚本删除所有文件从谷歌驱动器?,google-apps-script,google-drive-api,Google Apps Script,Google Drive Api,我需要从我的驱动器中删除超过16 GB的所有文件,我需要手动删除几个小时 寻求支持谷歌的帮助,但没有得到任何帮助 我可以移动我执行的Google Apps脚本吗?我假设您熟悉Google Apps脚本,知道如何在驱动器中创建脚本、管理编辑器等。。。如果你不是,请从这里开始 这里有一个小脚本,它会列出你所有的文件,并将它们设置为垃圾箱,你仍然需要去垃圾箱并永远删除 使用此脚本时要小心:将所有文件移到垃圾箱 运行此命令时,需要取消对文件.setTrashed(true)的注释 function pr
我可以移动我执行的Google Apps脚本吗?我假设您熟悉Google Apps脚本,知道如何在驱动器中创建脚本、管理编辑器等。。。如果你不是,请从这里开始 这里有一个小脚本,它会列出你所有的文件,并将它们设置为垃圾箱,你仍然需要去垃圾箱并永远删除 使用此脚本时要小心:将所有文件移到垃圾箱 运行此命令时,需要取消对文件.setTrashed(true)的注释
function processAllFiles() {
// we look for the continuation token from the UserProperties
// this is useful as the script may take more that 5 minutes
// (exceed execution time)
var continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
if (continuationToken == null) {
// firt time execution, get all files from drive
var files = DriveApp.getFiles();
// get the token and store it in a user property
var continuationToken = files.getContinuationToken();
UserProperties.setProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN', continuationToken);
} else {
// we continue to execute (and move everything to trash)
var files = DriveApp.continueFileIterator(continuationToken);
}
while (files.hasNext()) {
var file = files.next();
// file.setTrashed(true);
Logger.log(file.getName());
}
// finish processing delete the token
UserProperties.deleteProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
}
您可能会留下很多文件夹(如果它们是出于某种原因以编程方式创建的;),因此您可以运行这个小脚本将它们移动到垃圾箱中。不要忘记取消注释下面的行
function processAllFolder() {
// Log the name of every folder in the user's Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
Logger.log(folder.getName());
// folder.setTrashed(true);
}
};
让我知道这对你有什么好处。我对patt0(最佳)的答案非常感兴趣,并试图通过添加一些功能来改善它(只是一点点:-),以满足我个人的舒适感 以下是我的想法,仅供参考(添加的数据记录保存在一个不会被删除的文档中,这样您就可以跟踪发生了什么,或者如果您使用注释的
setTrashed()
,运行它会发生什么,并使用日志数据文档url向您发送邮件以便于访问)
结合两者的优秀工作,以及关于永久删除,我分享我自己的这个脚本的最终版本 首先,请确保按照Sandy Good答案中的说明执行,否则脚本将无法永久删除文件(尽管您仍然可以将文件丢弃) 脚本的特点:
- Stackdriver日志记录-每处理50个文件,就会有一条日志消息显示状态。
我建议添加严重性=跟踪进度时,筛选器出现错误
- 出错时跳过-有些文件将无法删除(最常见的是与您共享的文件,您不拥有这些文件,因此没有删除权限),这些文件将被记录
- Continuation
脚本在script.google.com上的运行时间不超过30分钟。
当此脚本失败/超时时,您将能够再次运行它,并且它将在停止的位置继续运行
- 未接触第三方应用程序数据,仍然需要手动删除。(一个占用空间的应用程序的好例子:)
要实际执行删除/删除,请取消对相关行的注释 祝你好运。
-著名的遗言 代码 也可通过此直接链接获得:()
日志Serge很好,我在我的基本版本中添加了一个处理文件夹的功能…很棒的工作人员,在这里运行了2个脚本并删除了所有文件。并且仍然生成了一个已删除文件的日志!太谢谢你了。很高兴这有帮助,这是一个有趣的例子:-)你的员工为你服务;)感谢文件夹更新(以及注释:-)(+1UPNote),当包含setTrashed()的行未注释时,这会不断生成服务器错误。我怀疑这与脚本文件删除本身有关,但@Sergeinsas answer也会在该行生成一个服务器错误。对我来说,这是开箱即用的(它节省了我一整天的工作-我有~50GB要删除)。嗯,我得到:找不到具有给定ID的项目,或者您没有访问它的权限。(第20行,文件“代码”)。第20行是:file.setTrashed(true);可能有什么问题?是否您不是要删除的文件的所有者?
function processAllFiles() {
var continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
var numberOfFiles = Number(UserProperties.getProperty('Number_of_files_processed'));
var thisScriptFileId = DocsList.find("continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN')")[0].getId();
Logger.log(thisScriptFileId);
if(UserProperties.getProperty('logFileId') == null ){
var logFileId = DocumentApp.create('Delete All Files Log data').getId();
var doc = DocumentApp.openById(logFileId);
doc.getBody().appendParagraph('List of all the files you deleted\n\n');
UserProperties.setProperty('logFileId', logFileId);
}
if (continuationToken == null) {
var files = DriveApp.getFiles();
var continuationToken = files.getContinuationToken();
UserProperties.setProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN', continuationToken);
UserProperties.setProperty('Number_of_files_processed', '0');
} else {
var files = DriveApp.continueFileIterator(continuationToken);
}
while (files.hasNext()) {
var file = files.next();
if(file.getId()!=logFileId&&file.getId()!=thisScriptFileId){
// file.setTrashed(true);
numberOfFiles++
Logger.log('File '+Utilities.formatString("%05d", numberOfFiles)+' : '+file.getName());
}
}
var paragraphStyle = {};
paragraphStyle[DocumentApp.Attribute.FONT_SIZE] = 8 ;
var doc = DocumentApp.openById(UserProperties.getProperty('logFileId'));
doc.getBody().appendParagraph(Logger.getLog()).setAttributes(paragraphStyle);
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'DeleteFiles result Log','Here is the log data to your script :\n\n'
+doc.getUrl()+'\n\nExecuted by this script : '+DocsList.getFileById(thisScriptFileId).getUrl());
// finish processing delete the token
UserProperties.deleteProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
UserProperties.deleteProperty('Number_of_files_processed');
}
// dont-delete-me-secret-code-1Nq0feuBuyGy5KWGqzEnvXODWx519Ka1aNSlXF_Bg6q1yP
// Link to this script: https://lksz.me/GoogleDriveCleaner
// Script based on the StackOverflow answers at:
// https://stackoverflow.com/a/25750738
// and https://stackoverflow.com/a/19616656 and https://stackoverflow.com/a/19615407
//
// You might need to run processAllFiles() multiple times.
// To start from scratch, first run clearContinuationToken()
//
// Last modified Nov 22, 2018
//
function processAllFiles() {
var usrP = PropertiesService.getUserProperties();
var continuationToken = usrP.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
var numberOfFiles = Number(usrP.getProperty('Number_of_files_processed'));
var numberOfErrors = Number(usrP.getProperty('Number_of_files_failed'));
var thisScriptFileId = DriveApp
.searchFiles('fullText contains "// dont-delete-me-secret-code-1Nq0feuBuyGy5KWGqzEnvXODWx519Ka1aNSlXF_Bg6q1yP"')
.next()
.getId();
Logger.log("thisScriptFileId = " + thisScriptFileId);
if (continuationToken == null) {
var files = DriveApp.getFiles();
var continuationToken = files.getContinuationToken();
usrP.setProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN', continuationToken);
usrP.setProperty('Number_of_files_processed', '0');
usrP.setProperty('Number_of_files_failed', '0');
} else {
var files = DriveApp.continueFileIterator(continuationToken);
}
while (files.hasNext()) {
var file = files.next();
var fileID = file.getId();
if(fileID!=thisScriptFileId){
try {
// Log advancement
if( 1 == (numberOfErrors + numberOfFiles) % 50 ) {
var msg = Utilities.formatString("%05d", numberOfFiles + numberOfErrors) + ', next file is: ' + file.getName();
console.log({message: msg, numberOfFiles: numberOfFiles, numberOfErrors: numberOfErrors, total: numberOfFiles + numberOfErrors });
Logger.log(msg);
usrP.setProperty('Number_of_files_processed', numberOfFiles);
usrP.setProperty('Number_of_files_failed', numberOfErrors);
}
// Un-comment one of the options below.
// Option 1: Permanent removal
// Follow instructions in https://stackoverflow.com/a/25750738 to enable Drive API
// Drive.Files.remove(fileID);
// Option 2: Trash file, will need to empty trash after script runs.
// file.setTrashed(true);
numberOfFiles++;
} catch (e) {
numberOfErrors++;
var msg = Utilities.formatString("%05d", numberOfFiles + numberOfErrors) + ', failed to remove file: ' + file.getName();
console.error({message: msg, numberOfFiles: numberOfFiles, numberOfErrors: numberOfErrors, total: numberOfFiles + numberOfErrors });
Logger.log(msg);
}
}
}
// finish processing delete the token
clearContinuationToken();
}
function clearContinuationToken() {
var usrP = PropertiesService.getUserProperties();
usrP.deleteProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
usrP.deleteProperty('Number_of_files_processed');
usrP.deleteProperty('Number_of_files_failed');
console.log({message: 'clearContinuationToken - Logging test', values: 1, testing: "bubu"});
}
function processAllFolder() {
// Log the name of every folder in the user's Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
console.log(folder.getName());
// folder.setTrashed(true);
}
}