Delete empty folders

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
vdongen
Posts: 49
Joined: 03 Apr 2008 06:57
Location: Jakarta, Indonesia

Delete empty folders

Post by vdongen »

Hi.

Possible to search for and delete empty folders?

Bart

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Delete empty folders

Post by zer0 »

Yes. Go to 'Edit' -> 'Find Files...'. Set the location where you want to search, tick 'Include subfolders'. Tick only the 'Size' filter on the right, below the search buttons. On the 'Size' tab, put 0 in the 'At the most' text box and tick 'Search for folders as well'. Do the search and list of empty folders will appear.

P.S. Empty files will be mentioned too, but it's easy to filter them out :)
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Delete empty folders

Post by serendipity »

I made a little script sometime back to do exactly this. Updated a bit now:

Code: Select all

//get folder sizes
   #487;
//select first item
   sel 1;
//empty clipboard
   copytext "";
//get position
   $pos=getinfo ("FocusedPos");
//get item count
   $items=getinfo ("Countitems");

//Loop until end  
   WHILE ($pos<=$items)
   {
//get type
   $type=property("Type","<curitem>");
//get size
   $size=report("{Size}",1);

//Has to be a folder and has to be empty
      IF ($type == "File folder" && $size == "-") 
      { 
//copy current item     
       copytext <curitem>|,a;
       incr $pos;
       sel $pos;
      }
      ELSE 
      {
      incr $pos;
      sel $pos;
      }
   }
//set clipboard to variable
   $clipboard=<clipboard>;
//remove trailing |
   $clipboard=substr ($clipboard, 0,-1);
//replace | with <crlf> for displaying text
   replace $clipboard, $clipboard, "|","<crlf>";
//display items to be deleted
   text $clipboard,,,"Items to be deleted";
//replace <crlf> with | for deletion
   replace $clipboard, $clipboard, "<crlf>","|";
//delete empty folders, move to recycle bin and prompt before delete
   delete 1,1, "$clipboard";
//
   status "Empty folders deleted";    

vdongen
Posts: 49
Joined: 03 Apr 2008 06:57
Location: Jakarta, Indonesia

Re: Delete empty folders

Post by vdongen »

Thanks guys.

Bart

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Delete empty folders

Post by serendipity »

Skyfrontier reported that this script no longer works, so here is the updated version.

Code: Select all

//Delete Empty folders
  #487;//Get Folder sizes 
  selfilter "Empty", d, "size";// Select empty folders
  focus;//Without this only one focused item is returned
  $items= get("SelectedItemsNames", "|+");//Get names of Selected items
  IF($items==""){
  status "No empty folders found.",,alert;
  end(1==1);
  }
  $tobedeleted= inputselect("To be deleted folders" ,+.$items, "|",2);//Shows list of folders (pre-checked) that will be deleted. You have the option to uncheck folders 
  selfilter "$tobedeleted";//Selects folders in list based on user's check/uncheck input
  delete 1, 1, :list;//Deletes folders to recycle bin, asks for confirmation. Remove both 1s to skip recycle and confirmation
I am amazed how much smaller this has become. Thanks to new commands.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Delete empty folders

Post by SkyFrontier »

Hmmmmm... Testing against test folders

Code: Select all

C:\test\New Folder-01\New Folder
C:\test\New Folder-01\New Folder-01
C:\test\New Folder-01\New Folder-02
C:\test\New Folder-01\New Folder-03
C:\test\New Folder-01\New Folder-04
with and without contents (sometimes a test file "New Textfile.txt", empty/zero size, sometimes with 1 char as content) I got random results like: deleting everything, deleting nothing, deleting 1 folder with/without test file. It's so erratic that I can't define a pattern and post a note about it.
-can someone please confirm?
>I made an update at the Library under the tag "Delete Empty Folders" and left a note regarding the problem, anyway.
Thank you!

EDIT: Tested on v.9.60.0009 (current beta).
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Delete empty folders

Post by SkyFrontier »

Worst problem so far: it tells correctly which folder(s) will delete but ends deleting everything (=all folders, with and without content!), in fact.
-erratically, too.

EDIT: Even worse: the code do makes sense... :roll:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

serendipity
Posts: 3358
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Delete empty folders

Post by serendipity »

Sorry, there is a small bug. Fixing it. will post back soon.
Edit: here it is again:

Code: Select all

//Delete Empty folders
  #487;//Get Folder sizes 
  selfilter "Empty", d, "size";// Select empty folders
  focus;//Without this only one focused item is returned
  $items= get("SelectedItemsNames", "|+");//Get names of Selected items
  IF($items==""){
  status "No empty folders found.",,alert;
  end(1==1);
  }
  $tobedeleted= inputselect("To be deleted folders" ,+.$items, "|",2);//Shows list of folders (pre-checked) that will be deleted. You have the option to uncheck folders 
  $char=chr(34);
  $tobedeleted=replace ($tobedeleted, "|","$char|$char");
  $tobedeleted=$char.$tobedeleted.$char;

  selfilter "$tobedeleted";//Selects folders in list based on user's check/uncheck input
  delete 1, 1, :list;//Deletes folders to recycle bin, asks for confirmation. Remove both 1s to skip recycle and confirmation

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Delete empty folders

Post by SkyFrontier »

Tested, working, Library updated.
Many thanks!

Note: it must be used without any folder being selected, otherwise script will consider only selected/items displaying [empty] as per folder size.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Delete empty folders

Post by SkyFrontier »

Let's suppose I want to check all subfolders of a folder. Am I doing any dumbness using the script like this? Any potential damages involved...?
(using the "quote" mode to highlight change; consider adding blank spaces to get the script fully functional!)
Decent version:

Code: Select all

//Delete Empty Folders
   #263;//all items on a branch
   #487;//Get Folder sizes
   selfilter "Empty", d, "size";// Select empty folders
   focus;//Without this only one focused item is returned
   $items= get("SelectedItemsNames", "|+");//Get names of Selected items
   IF($items==""){
   status "No empty folders found.",,alert;
   end(1==1);
   }
   $tobedeleted= inputselect("To be deleted folders" ,+.$items, "|",2);//Shows list of folders (pre-checked) that will be deleted. You have the option to uncheck folders
   $char=chr(34);
   $tobedeleted=replace ($tobedeleted, "|","$char|$char");
   $tobedeleted=$char.$tobedeleted.$char;

   selfilter "$tobedeleted";//Selects folders in list based on user's check/uncheck input
   delete 1, 1, :list;//Deletes folders to recycle bin, asks for confirmation. Remove both 1s to skip recycle and confirmation
//Delete Empty folders
#263;//all items on a branch
#487;//Get Folder sizes
selfilter "Empty", d, "size";// Select empty folders
focus;//Without this only one focused item is returned
$items= get("SelectedItemsNames", "|+");//Get names of Selected items
IF($items==""){
status "No empty folders found.",,alert;
end(1==1);
}
$tobedeleted= inputselect("To be deleted folders" ,+.$items, "|",2);//Shows list of folders (pre-checked) that will be deleted. You have the option to uncheck folders
$char=chr(34);
$tobedeleted=replace ($tobedeleted, "|","$char|$char");
$tobedeleted=$char.$tobedeleted.$char;

selfilter "$tobedeleted";//Selects folders in list based on user's check/uncheck input
delete 1, 1, :list;//Deletes folders to recycle bin, asks for confirmation. Remove both 1s to skip recycle and confirmation
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Nasri
Posts: 10
Joined: 23 Mar 2017 04:56

Re: Delete empty folders

Post by Nasri »

zer0 wrote: 29 Dec 2009 17:13 Yes. Go to 'Edit' -> 'Find Files...'. Set the location where you want to search, tick 'Include subfolders'. Tick only the 'Size' filter on the right, below the search buttons. On the 'Size' tab, put 0 in the 'At the most' text box and tick 'Search for folders as well'. Do the search and list of empty folders will appear.

P.S. Empty files will be mentioned too, but it's easy to filter them out :)
Thank you, this works for me! :)

RalphM
Posts: 1932
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Delete empty folders

Post by RalphM »

Surely a long awaited thanks - almost 9 years in the making. :whistle:
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

Nasri
Posts: 10
Joined: 23 Mar 2017 04:56

Re: Delete empty folders

Post by Nasri »

zer0 wrote: 29 Dec 2009 17:13 Yes. Go to 'Edit' -> 'Find Files...'. Set the location where you want to search, tick 'Include subfolders'. Tick only the 'Size' filter on the right, below the search buttons. On the 'Size' tab, put 0 in the 'At the most' text box and tick 'Search for folders as well'. Do the search and list of empty folders will appear.
P.S. Empty files will be mentioned too, but it's easy to filter them out :)
Still a classic, albeit manual, it just works for me on v21.10 :D

Post Reply