我需要删除驱动器中的所有文件,超过16 GB,并且我需要手动删除数小时。
寻求帮助以支持谷歌并且没有任何帮助。
我可以移动我执行的Google Apps脚本吗?
答案 0 :(得分:9)
我将假设您熟悉Google Apps脚本,以便您知道如何在驱动器中创建脚本,管理编辑器等......如果您不熟悉,请从https://developers.google.com/apps-script/overview开始。
这里有一个小脚本,它会列出你的所有文件并将它们设置为垃圾箱,你仍然需要去垃圾桶并永久删除。
使用此脚本时要小心:移动所有文件以备份
运行此
时,您需要取消注释file.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);
}
};
让我知道这对你有用。
答案 1 :(得分:4)
我对patt0(最好)的答案非常感兴趣并尝试通过添加一些功能来改善它(只是一点点:-)以便我个人的安慰......
以下是我的观点,仅供参考(添加的数据记录保存在单个文档中,不会被删除,因此您可以跟踪发生的事情 - 或者如果您运行它会发生什么评论 setTrashed()
- 并使用日志数据doc url向您发送邮件以便于访问)
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');
}
答案 2 :(得分:0)
结合patt0和Serge insas的出色工作以及Sandy Good's answer关于永久删除的知识,我将分享此脚本的最终版本。
首先,请确保按照 Sandy Good 的回答中的说明进行操作,否则,脚本将无法永久删除文件(尽管您仍然可以删除文件)。
severity!=ERROR
添加到过滤器中。危险地带
此脚本将在其必须运行的指定时间内删除所有可能的内容,因此,我已注释掉了文件的实际删除/删除(与其他答案相同)。
要实际执行删除/删除操作,请取消注释相关行。祝你好运。
-著名的遗言
也可以通过以下直接链接获得:(https://lksz.me/GoogleDriveCleaner)
// 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);
}
}