2
votes

During our migration to Team Drives, we had a folder that was migrated twice. The first time was interrupted, and we now have thousands of duplicate folders with nothing in them. I would like to delete or at least remove any folders on a specific team drive that are completely empty. I found an old script that I tried to modify below:

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

}

}

}

My question is how do I get this script to run on a specific team drive, or better yet, in a specific folder on a team drive?

Original Question answered in comments - new script below - followup question in comments

Thank you in advance for your help and my apologies as I am not well experienced in coding or scripts.

 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);
2
See my answer to browse files in google team drive. It may help you.Rubén
Thank you very much! Using your help I was able to write the above script (at the bottom of original post, ran out of characters in comment), for those who may be searching the same thing:Andrew Freeman
My followup question (not sure if I should make a new topic) is that between my ///////// marks I tried to make the script get sub-sub folders (second level), however it keeps telling me: TypeError: Cannot find function getFolders in object FolderIterator. Is there a better way to get it to list the sub-subfolders in the spreadsheet?Andrew Freeman

2 Answers

1
votes

Here is a modified function that will recursively delete empty folders and log them to a spreadsheet.

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

I made a slight modification to @Brett's answer because it traverses the directory tree in a top-down order instead of a bottom-up order. I had a lot of nested empty subdirectories and wanted to delete them all, so it was better for me to (re)check filecount/foldercount after the recursion so it takes into account deeper-level deletion. I also sped it up a little by not caring about the filecount or foldercount, just if there exists a file or not

Also, if you want to remove the folders, I think it's better to use .setTrashed so it is trashed, as opposed to .removeFolder which just moves it to a hidden top level directory

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