• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Remove empty folders and Subfolders

Community Beginner ,
Jan 10, 2018 Jan 10, 2018

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jan 10, 2018 Jan 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.numIte

...

Votes

Translate

Translate
Enthusiast ,
Jan 10, 2018 Jan 10, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines