2

在我们迁移到团队云端硬盘期间,我们有一个文件夹被迁移了两次。第一次被打断了,我们现在有数千个重复的文件夹,里面什么都没有。我想删除或至少删除特定团队驱动器上完全空的任何文件夹。我在下面找到了一个尝试修改的旧脚本:

   function delEmptyFolders() {

var folders = DriveApp.getFolders();

var ss = SpreadsheetApp.create("folderslist");

while (folders.hasNext()) {

var folder = folders.next();

var files = folder.getFiles();

var childfolders = folder.getFolders();

var filecount = 0;

var foldercount = 0;

while (childfolders.hasNext()){

var childfolder = childfolders.next();

foldercount ++;

}

while (files.hasNext()){

var file = files.next();

filecount ++;

}

ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount]);

if (filecount == 0 && foldercount == 0){

folder.setTrashed(true);

}

}

}

我的问题是如何让这个脚本在特定的团队驱动器上运行,或者更好的是,在团队驱动器的特定文件夹中运行?

评论中回答的原始问题 - 下面的新脚本 - 评论中的后续问题

提前感谢您的帮助和我的歉意,因为我在编码或脚本方面经验不足。

 function starEmptyFolders() {

     var folders = DriveApp.getFolderById('ID').getFolders();


var ss = SpreadsheetApp.create("test");

while (folders.hasNext()) {

var folder = folders.next();

var files = folder.getFiles();

var childfolders = folder.getFolders();

var filecount = 0;

var foldercount = 0;

while (childfolders.hasNext()){

var childfolder = childfolders.next();

foldercount ++;

}

while (files.hasNext()){

var file = files.next();

filecount ++;
}

  ////////////////////
var child = childfolders;
  var subFolders = child.getFolders();
  while(subFolders.hasNext()) {
    var subFolder = subFolders.next();
    var subfoldercount = 0;
    subfoldercount ++}




  ///////////////////////////
}
  ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount, subfoldercount,]);

if (filecount == 0 && foldercount == 0){

folder.setStarred(true);
4

2 回答 2

1

这是一个修改后的函数,它将递归删除空文件夹并将它们记录到电子表格中。

function deleteEmptyFolders() {
  var parentFolder = DriveApp.getFolderById('<folder ID goes here>');
  var folders = parentFolder.getFolders();
  var ss = SpreadsheetApp.create("ScriptDeletes");
  while (folders.hasNext()) {
    var childfolder = folders.next();
    recurseFolder(parentFolder, childfolder, ss);
  }
}

function recurseFolder(parentFolder, folder, ss) {
        var filecount = 0;
        var foldercount = 0;

        var files = folder.getFiles();               // get the list of files in the root of that folder
        var childfolders = folder.getFolders();      // get the list of sub folders in the root of that folder

        while (childfolders.hasNext()) {               // count the sub folders
            var childfolder = childfolders.next();
            recurseFolder(folder, childfolder, ss);
            foldercount++;
        }

        while (files.hasNext()) {                      // count the files in the root of the folder
            var file = files.next();
            filecount++;
        }

       if (filecount == 0 && foldercount == 0) {            
            parentFolder.removeFolder(folder);
            ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount]);
       }             
}
于 2019-07-16T19:31:55.843 回答
0

我对@Brett 的回答做了一些修改,因为它以自上而下的顺序而不是自下而上的顺序遍历目录树。我有很多嵌套的空子目录并想将它们全部删除,所以我最好在递归之后(重新)检查文件计数/文件夹计数,以便考虑更深层次的删除。我还通过不关心文件数或文件夹数来加快它的速度,只要是否存在文件

另外,如果你想删除文件夹,我认为最好使用.setTrashed它,这样它就会被丢弃,而不是.removeFolder仅仅将它移动到隐藏的顶级目录

function deleteEmptyFolders() {
  var parentFolder = DriveApp.getFolderById('FOLDER_ID');
  var folders = parentFolder.getFolders();
  var ss = SpreadsheetApp.openById("SPREADSHEET_ID")
  while (folders.hasNext()) {
    var childfolder = folders.next();
    recurseFolder(parentFolder, childfolder, ss);
  }
}

function recurseFolder(parentFolder, folder, ss) {


  var childfolders = folder.getFolders();      // get the list of sub folders in the root of that folder

  while (childfolders.hasNext()) {               // count the sub folders
    var childfolder = childfolders.next();
    recurseFolder(folder, childfolder, ss);
  }

  var hasFile = folder.getFiles().hasNext();               // get the list of files in the root of that folder
  var hasFolder = folder.getFolders().hasNext();               // get the list of files in the root of that folder

  if (!hasFile && !hasFolder) {            
    folder.setTrashed(true)
    ss.appendRow([folder.getId(),folder.getName()]);
  }             
}
于 2020-05-11T17:11:50.297 回答