Highlighted

Remove empty folders and Subfolders

Community Beginner ,
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()}

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

TOPICS
Scripting

Views

1.4K

Likes

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

Remove empty folders and Subfolders

Community Beginner ,
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()}

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

TOPICS
Scripting

Views

1.4K

Likes

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
Jan 10, 2018 0
Engaged ,
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);

Likes

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
Reply
Loading...
Jan 10, 2018 0
Community Beginner ,
Jan 11, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jan 11, 2018 0