10

我需要从我的驱动器中删除所有超过 16 GB 的文件,并且我需要花费数小时手动删除。

寻求帮助以支持谷歌,但没有任何帮助。

我可以移动我执行的 Google Apps 脚本吗?

4

3 回答 3

8

我假设您已经熟悉 Google Apps 脚本,以至于您知道如何在驱动器中创建脚本、管理编辑器等……如果您不熟悉,请从这里开始https://developers.google.com /应用程序脚本/概述

这里有一个小脚本,它将列出您的所有文件并将它们设置为垃圾箱,您仍然需要进入垃圾箱并永远删除。

使用此脚本时要小心:将所有文件移至垃圾箱

运行此命令时,您需要取消注释 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);
  }
};

让我知道这对你有什么影响。

于 2013-10-27T06:48:57.403 回答
4

我对 patt0 的(最佳)答案非常感兴趣,并尝试通过添加一些功能来改进它(只是一点点 :-) 以使我个人舒适......

这是我来的,仅供参考(添加的数据记录保存在一个不会被删除的文档中,这样您就可以跟踪发生的事情 - 或者如果您使用注释运行它会发生什么 setTrashed()- 并发送使用日志数据文档 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');
}
于 2013-10-27T09:42:51.930 回答
0

结合patt0Serge insas的出色工作以及Sandy Good关于永久删除的回答,我将分享我自己的这个脚本的最终版本。

首先,请确保遵循Sandy Good回答中的说明,没有它,脚本将无法永久删除文件(尽管您仍然可以将文件删除)。

脚本特点:

  • Stackdriver Logging - 每处理 50 个文件,就会有一条日志消息与状态有关。
    我建议添加severity!=ERROR到过滤器中,同时跟踪进度。
  • 出错时跳过- 有些文件无法删除(最常见的是与您共享的文件,您不拥有这些文件,因此无权删除),这些文件将被记录。

  • script.google.com 上的延续脚本的运行时间不得超过 30 分钟。
    当此脚本失败/超时时,您将能够再次运行它,并且它将从停止的地方继续。

这个脚本不做什么:

  • 未触及第 3 方应用程序数据,仍需要手动删除。(占用空间的应用程序的一个很好的例子:WhatsApp 备份

危险区

该脚本将在必须运行的分配时间内删除它可以删除的所有内容,因为我已经注释掉了文件的实际删除/垃圾(与其他答案相同)。
要实际执行删除/垃圾处理,请取消注释相关行。

祝你好运。
- 著名遗言

编码

也可通过此直接链接获得:(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);
      }
    }
于 2018-11-23T05:04:35.400 回答