还原将谷歌驱动器文件丢弃到目标文件夹 [英] Restore trashed google drive files to a target folder

查看:83
本文介绍了还原将谷歌驱动器文件丢弃到目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将目前在我的谷歌驱动器垃圾桶中的所有文件移动到某个文件夹,或者如果无法完成,请将它们恢复到原始位置。发生了一些混乱事件,我在垃圾箱中找到了有价值的文件,其中有6个文件,因此我宁愿将它们移动到单独的目录中,备份或本地同步,以后再查看这些文件。



这是我到目前为止的脚本:

  function moveFilesFromTrash(){
var pageSize = 200;
var files = null;
var token = null;

var cestisti = DocsList.getFolder('cestisti');

do {
var result = DocsList.getAllFilesForPaging(pageSize,token);
files = result.getFiles();
token = result.getToken();
for(var i = 0; i< files.length; i ++){
if(files [i] .isTrashed == true){
Logger.log(files [i] .getName());
//文件[i] .setTrashed(false)
文件[i] .addToFolder(cestisti);
}
} while(files.length == pageSize);
}

问题在于它不起作用。



通过文件项目进行页面切换的代码部分起作用,我从我的其他工作脚本中获得了它。我只是不知道它是否解析垃圾文件夹或标签。我不知道是否 setTrashed() addtofolder()的作品 - 我不知道。现在前者已被注释掉,因为我宁愿复制项目而不是恢复项目,但如果这是不可能的,我可以恢复。

解决方案

p> 由于DocsList的弃用,此答案已更新,现在全部使用DriveApp方法。 在移动文件之前,您必须清除文件,但这样做会完成您的工作。以下是算法:


  1. 获取所有已删除文件的文件列表(或文件标识符)

  2. 对于每个文件,


    • 取消删除/取消垃圾文件
    • 将文件移动到目标文件夹(detail:remove文件从原始文件夹,添加到目标恢复文件夹)


这个答案提供片段脚本来完成这个;完整的脚本可以在



进程已删除文件



现在,被删除文件的列表位于电子表格中,编写恢复&移动很容易。


$ b

/ **
* Untrash然后将电子表格中列出的文件移至rescue文件夹。
*来自:http://stackoverflow.com/a/14541247/1677912
*
* @returns {Number}获救文件数。
* /
函数rescueListedFiles(){
var fileList = SpreadsheetApp.getActiveSheet()
.getDataRange()。getValues()
.splice(1); //跳过标题行
//查找或创建目标文件夹
var rescueFldrIterator = DriveApp.getFoldersByName(rescueFldrName);
var rescueFldr = rescueFldrIterator.hasNext()? rescueFldrIterator.next():DriveApp.createFolder(rescueFldrName);

var count = 0;
for(var i = 0; i< fileList.length; i ++){
var fileId = fileList [i] [0];
var file = DriveApp.getFileById(fileId);
//解压文件
if(rescueFile(file,rescueFldr))count ++;
}
return(count);
};


I need to move all files presently in my google drive's trash to a certain folder or, if that can't be done, restore them to their original location. Some mess happened and I have valuable files in the trash, some 6 gig of them, so I'd prefer to move them to a separate directory, back it up or sync locally and see those files later.

This is the script I have so far:

function moveFilesFromTrash() {
  var pageSize = 200;
  var files = null;
  var token = null;

  var cestisti = DocsList.getFolder('cestisti');

  do {
    var result = DocsList.getAllFilesForPaging(pageSize, token);
    files = result.getFiles();
    token = result.getToken();
    for (var i = 0; i < files.length; i++) {
      if (files[i].isTrashed == true) {
        Logger.log(files[i].getName());
//      files[i].setTrashed(false)
        files[i].addToFolder(cestisti);
    }
  } while (files.length == pageSize);
}

The matter is it just does not work.

The part of the code to page through files items works, I got it from other working scripts of mine. I just don't know if it parses the trash folder or label to. I do not know if setTrashed() or addtofolder() works - I have no idea. Now the former is commented out because I would prefer to copy items instead of restoring them, but if that's not possible I can restore.

解决方案

This answer has been updated due to the deprecation of DocsList, and now uses DriveApp methods throughout.

You must un-trash a file before you can move it, but doing so will accomplish what you're after. Here's the algorithm:

  1. Get a list of files (or file ids) for all trashed files
  2. For each file,
    • Undelete / un-trash the file
    • Move file to target folder (detail: remove file from original folders, add to target recovery folder)

This answer provides snippets of script to accomplish this; The full script is available as a Google Sheets Add-on in a gist, complete with menu and UI elements.

Recover all trashed files

This function uses DriveApp.search() to get the list of all trashed files, and then recovers them into a specified folder, rescueFldr, which will be created if not already there.

/**
 * Untrash then move all trashed files to rescue folder.
 * From: http://stackoverflow.com/a/14541247/1677912
 * 
 * @returns {Number}   Count of Rescued files.
 */
function rescueAllFiles() {
  // Find or create target folder
  var rescueFldrIterator = DriveApp.getFoldersByName(rescueFldrName);
  var rescueFldr = rescueFldrIterator.hasNext() ? rescueFldrIterator.next() : DriveApp.createFolder(rescueFldrName);

  // Get file iterator with all trashed files
  var trashed = DriveApp.searchFiles('trashed=true');

  var count = 0;
  while (trashed.hasNext()) {
    var file = trashed.next();
    // Untrash the file
    if (rescueFile( file, rescueFldr )) count++;
  }
  return(count);
};

Recover specific Trashed Files

If you want to have more control over the files that will be recovered, and you can't simply modify the search parameters used above, an alternative might be to list the file IDs in a spreadsheet, and have a script use that as its input.

Fill spreadsheet with trashed files

/**
 * Get array of files in user's Google Drive trash, and write
 * into currently active sheet.
 * From: http://stackoverflow.com/a/14541247/1677912
 * 
 * @returns   {Object[]}   Array of DriveApp file objects
 */
function getTrashedFiles() {
  var trashedSearch = DriveApp.searchFiles('trashed=true');
  var files = [];
  files.push(["ID","File Name","Type","URL"]);
  while (trashedSearch.hasNext()) {
    var file = trashedSearch.next();
    files.push([file.getId(),file.getName(),docTypeToText_(file.getMimeType()),file.getUrl()]);
  }
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.getRange(1, 1, files.length, files[0].length)
       .setValues(files);
  return files;
}

Manual file ID collection

(Alternative to automatic retrieval, for reference only.) Since you're just doing this once, though, let's cheat. At the bottom of the Files-list page, use the API Explorer to retrieve the IDs of all your deleted files!

The output is shown just as you'd receive it if you were calling the API from a script.

Cut & paste the output into a text file, and strip it down to just the file ids. (I did that with gvim, it takes just a few commands.) Save the result as a csv file, and pull it into Drive as a spreadsheet. Here's what that will look like. (Do include a header line; the script assumes there is one.)

Process Trashed Files

Now that the list of trashed files is in a spreadsheet, scripting the recovery & move is easy.

/**
 * Untrash then move files listed in spreadsheet to rescue folder.
 * From: http://stackoverflow.com/a/14541247/1677912
 * 
 * @returns {Number}   Count of Rescued files.
 */
function rescueListedFiles() {
  var fileList = SpreadsheetApp.getActiveSheet()
                               .getDataRange().getValues()
                               .splice(1);  // Skip header line
  // Find or create target folder
  var rescueFldrIterator = DriveApp.getFoldersByName(rescueFldrName);
  var rescueFldr = rescueFldrIterator.hasNext() ? rescueFldrIterator.next() : DriveApp.createFolder(rescueFldrName);

  var count = 0;  
  for (var i=0; i<fileList.length; i++) {
    var fileId = fileList[i][0];
    var file = DriveApp.getFileById(fileId);
    // Untrash the file
    if (rescueFile( file, rescueFldr )) count++;
  }
  return( count );
};

这篇关于还原将谷歌驱动器文件丢弃到目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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