Skip to main content
detailed_musician15C3
Known Participant
January 10, 2018
Answered

Remove empty folders and Subfolders

  • January 10, 2018
  • 1 reply
  • 3608 views

Hello i'm trying to clean my project .i have a lot of empty folder and folder with subfolder inside ....

Sometimes,i have many level off subfolders empty inside one and i try to delete them.

i try this code, its work for simple folder, but i still have folder with subfolder inside and i cant delete this kind of files ....

any idee ??

Thank you everyone...

//remove emty folders

for (var i = 1; i<=app.project.numItems; i++) {

    if(app.project.item(i) instanceof FolderItem& app.project.item(i).numItems==0)

    app.project.item(i).remove()}

This topic has been closed for replies.
Correct answer Paul Tuersley

For this you need what's called a recursive function. The function keeps calling itself, working all the way down the chain of folders until it gets to the last subfolder with no contents and removes it. Then as you fall back through each recursion, any folders that now contain no items (because the subfolders have just been removed) will also be deleted.

function removeFolders(theFolder) {

  // loop from last to first item so removing a folder won't affect the index

  for (var i = theFolder.numItems; i > 0; i--) {

  if (theFolder.item(i) instanceof FolderItem) {

  // if subfolder contains items, enter recursive function again using the subfolder

  if (theFolder.item(i).numItems > 0) removeFolders(theFolder.item(i));

  // if either it never contained any items, or the contents has now been removed after leaving the later recursions, delete the folder

  if (theFolder.item(i).numItems == 0) theFolder.item(i).remove();

  }

  }

}

// enter function for the first time using the root folder

removeFolders(app.project);

1 reply

Paul TuersleyCorrect answer
Inspiring
January 10, 2018

For this you need what's called a recursive function. The function keeps calling itself, working all the way down the chain of folders until it gets to the last subfolder with no contents and removes it. Then as you fall back through each recursion, any folders that now contain no items (because the subfolders have just been removed) will also be deleted.

function removeFolders(theFolder) {

  // loop from last to first item so removing a folder won't affect the index

  for (var i = theFolder.numItems; i > 0; i--) {

  if (theFolder.item(i) instanceof FolderItem) {

  // if subfolder contains items, enter recursive function again using the subfolder

  if (theFolder.item(i).numItems > 0) removeFolders(theFolder.item(i));

  // if either it never contained any items, or the contents has now been removed after leaving the later recursions, delete the folder

  if (theFolder.item(i).numItems == 0) theFolder.item(i).remove();

  }

  }

}

// enter function for the first time using the root folder

removeFolders(app.project);

detailed_musician15C3
Known Participant
January 11, 2018

Thank you so much,it works fine !!!

i didn't know the recursive function ,i will gonna explore this technics for other fonctions ...

Thanks Paul