删除空文件夹团队驱动器

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (4)

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

   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);
提问于
用户回答回答于

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

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]);
       }             
}

扫码关注云+社区

领取腾讯云代金券