How to? Recursivly delete empty folders

Pieced this together as I was looking to clean up.

alias CleanDirs = del /s/e/x/y /nest THISISJUSTAPLACEHOLDER.NOTAREALFILENAME

Hope it makes it into the Google Search machine and helps someone else!
And... the order of the command line switches makes it easy to remember ... SEXY NEST :cool:
(ya.... at times I'm still a 13 year old kid!)
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,423
85
Albuquerque, NM
prospero.unm.edu
I've used del /s /e /x /y con.* — knowing that Windows will not allow filenames to match a device name.

And I've seen others using something like del /a:d /s /e /x /y * on the theory that DEL deletes files, and no file will have the Directory attribute set. (Test with caution; I'm not certain of the syntax for that approach.)

Want to prevent the top-level directory from being removed? Switch to it with PUSHD before your DEL, and POPD out afterwards. You can't remove the current directory.
 
  • Like
Reactions: Green Weenie
Hi Charles,

I didn't expect a reply, but thanks for affirming that I'm not the only one who still loves the power of TCC in 2021! :)
And... I am more mature than you might gather from my nickname... not much... but ;)

del /s/e/x/y /nest THISISJUSTAPLACEHOLDER.NOTAREALFILENAME

I didn't mention one of the reasons I loved the above version is it gives very nice clean output of what directories have been removed.

Run this for a sample, anyplace on your drive. First two commands create a mess of empty directories.
md ThisIsATest\Some Directories have Spaces\But this Shouldn't matter\Ohhh a quoted directory name!\and other nonsense
md "ThisIsATest\Some Directories have Spaces\But this Shouldn't matter\Ohhh a quoted directory name!\and other nonsense"
del /s/e/x/y /nest THISISJUSTAPLACEHOLDER.NOTAREALFILENAME

This is what the output looks like:
Removing E:\a\
Removing E:\Directories\
Removing E:\directory\
Removing E:\have\
Removing E:\matter\Ohhh\
Removing E:\matter\
Removing E:\name!\and\
Removing E:\name!\
Removing E:\nonsense\
Removing E:\other\
Removing E:\quoted\
Removing E:\Shouldn't\
Removing E:\Spaces\But\
Removing E:\Spaces\
Removing E:\this\
Removing E:\ThisIsATest\Some\
Removing E:\ThisIsATest\Some Directories have Spaces\But this Shouldn't matter\Ohhh a quoted directory name!\and other nonsense\
Removing E:\ThisIsATest\Some Directories have Spaces\But this Shouldn't matter\Ohhh a quoted directory name!\
Removing E:\ThisIsATest\Some Directories have Spaces\But this Shouldn't matter\
Removing E:\ThisIsATest\Some Directories have Spaces\
Removing E:\ThisIsATest\
So nice and easy!
Try doing that with a CMD prompt!
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,423
85
Albuquerque, NM
prospero.unm.edu
Cybertool.jpg

Its uses are limited only by your own imagination.
 
  • Like
Reactions: Green Weenie
Apr 2, 2011
1,404
10
54
North Carolina, USA
Hi Charles,

I didn't expect a reply, but thanks for affirming that I'm not the only one who still loves the power of TCC in 2021! :)
And... I am more mature than you might gather from my nickname... not much... but ;)

del /s/e/x/y /nest THISISJUSTAPLACEHOLDER.NOTAREALFILENAME

I didn't mention one of the reasons I loved the above version is it gives very nice clean output of what directories have been removed.

Run this for a sample, anyplace on your drive. First two commands create a mess of empty directories.


This is what the output looks like:

So nice and easy!
Try doing that with a CMD prompt!

I would assume one would want to update the CDD extended list and not use "/Ne" .... but I guess everyone's milage would vary....
 
  • Like
Reactions: Green Weenie
@Charles Dye - it might have been me with the DEL /a:d /s /e /x /y *

The /a:d is a nice additional safety feature. Thanks!
I strongly recommend against using the * wildcard, vs a fictitious file name.
Murphy wrote a law that covers this... That DEL * is just an accident waiting to happen! :-P

Oh ... and as an indication regarding Murphy's Law in my life...
I was surprised this morning when I typed cleandirs and got an Unknown command response.
Oh ya... I didn't add the alias to the startup :banghead: hehehehehe