in google-apps-script is there a way to get to know if a google drive's folder is empty?

Refresh

December 2018

Views

2k time

2

I would like to write a google apps script to trash every empty folder into my google drive, which method / property should I use to examine the current folder (I can write a cycle) is really empty, id est without any file or any other nested folder inside?

And, what happens if you trash a folder which has some files linked to? Do those files get trashed to or do they get orphaned (if linked to just on folder, of course)?

Thank you.

–
cordialmente,

tiziano solignani, da  Mac http://blog.solignani.it

This is what I coded after the first useful answer, I just inserted pagination:

function FindEmptyFolders() {

var pageSize = 200;
var folders = null;
var token = null;
var i = null;

Logger.clear()

do {
var result = DocsList.getAllFoldersForPaging(pageSize, token);
folders = result.getFolders();
token = result.getToken();
  for (var i = 0; i < folders.length; i++) {
      for(n=0;n<folders.length;++n){
        if(folders[n].getFiles().length==0 && folders[n].getFolders().length==0){
            folders[n].setTrashed(true)
            Logger.log(folders[n].getName())
            Logger.log(folders[n].getUrl())
        }
      }    
  } 
 } while (folders.length == pageSize);

  MailApp.sendEmail('[email protected]', 'Script eliminazione folders vuoti Log', Logger.getLog());

} 

1 answers

2

этот код делает работу

function testForEmptyFolders() {
var folders = DocsList.getAllFolders()
  for(n=0;n<folders.length;++n){
    if(folders[n].getFiles().length==0 && folders[n].getFolders().length==0){
     folders[n].setTrashed(true)
     Logger.log(folders[n].getName())
     }
   }
}

Регистратор показывает имена удаленных папок