Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Clean out empty files and folders

Post image for Clean out empty files and folders

by Steve Wiseman on April 26, 2012 · 20 comments

in BAT Files,Tips,Utility


.

A few months ago I received an email from Steve in Ontario:

“…I want to round out this junk cleaning by trashing empty directories as well. I have found all sorts of approaches to this, but none are elegant. I am old-school, having started as a UNIX system programmer some … 40 years ago:-) So, I like elegant solutions, and in UNIX, this would be a simple one-line piped solution … unfortunately, I’ve been stuck on Windows for the past few decades…I’m wondering if you’ve ever tackled this, and, if so, if there’s anything you can share?”

In my arrogance I thought this would be such an easy task, and coded a utility in less than an hour. Sent it off to Steve to try, and when he emailed me back I realized how tough of a problem this is.

For example, if you were looking to remove empty directories from this structure:

c:\Folder1\Folder2\EmptyFolder1
c:\Folder1\Folder2\EmptyFolder2

It would be easy to find EmptyFolder1, and EmptyFolder2. But now that they are removed, Folder2 is empty as well. This means you need to delete the deepest paths first, and work backwards.

Lots of special cases like this, and that is what makes it much more difficult than I originally thought.

Over a few weeks, Steve was gracious enough to work with me and develop a solution that worked well.

The utility is called DelEmpty.exe, a command line tool:

DelEmpty Screen Shot

Here are the options:

DelEmpty.exe OPTIONS [PATH]

-f delete files of size zero
-d delete empty directories
-v verbose mode
-c confirm mode (Shows what was deleted)
-s include sub directories
-l list what would be deleted (Will not delete)
-y delete without (y/n) prompt

So, for example, if we wanted to remove all empty folders from c:\DataFolder, including sub folders – We would call it like this:

DelEmpty.exe -d -s c:\datafolder

If you wanted it to delete them without prompting you:

DelEmpty.exe -d -s -y c:\datafolder

Better yet, if we wanted to see what it would delete before taking any action:

DelEmpty.exe -d -s -l c:\datafolder

Finally, we can also remove any files that have a zero size too using the -f option:

DelEmpty.exe -d -s -y -f c:\datafolder

Please – Please be careful with this utility. It only deletes empty files and folders, but it is always possible that you have a program that *needs* those empty folders. So make sure you throughly test before using it.

It is a free utility, and you can get it from our download page:

IntelliAdmin Download Page

As always, there is no spyware, adware – only IntelliAdmin Goodness.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 20 comments… read them below or add one }

1 Dave W April 26, 2012 at 11:48 pm

Great tool steve. I really needed something like this – got a program here that creates folders named after the current date, and then leaves them behind. This will be a perfect way to keep that folder clean now.

2 Jennifer April 26, 2012 at 11:50 pm

Thanks for the freebie steve.

3 Yogesh April 27, 2012 at 2:14 am

DelEmpty.exe dosent work in windows 7.plz tell me how its work….

4 David Chang April 27, 2012 at 10:05 am

Can this be run from a bat file? I see that it keeps prompting me if I want to remove a file

5 Steve Wiseman April 27, 2012 at 10:06 am

Sure it does. That is the version of Windows that I mostly tested and built it on. What type of error are you getting?

Thanks,

Steve

6 Steve Wiseman April 27, 2012 at 10:07 am

Yes David, you just need to use the -y parameter

7 David Chang April 30, 2012 at 9:33 am

Got it all working steve from a bat file. Great utility. Thank you so much for making this free!

8 Steve Wiseman April 30, 2012 at 9:34 am

Glad to hear it David. Email me if you have any more issues getting to work, and I will be glad to help.

9 Srinivasulu May 17, 2012 at 10:51 am

It’s very nice utility.
I would like to have a BAT file of this job.
Is it available with you
seena

10 Patrick June 7, 2012 at 9:38 am

Can’t seem to get it to work on network drives. Cmd is being run as:

delempty.exe -d -s -l h:\

But comes up with no empty folders to delete. I know there are empty folders as I’ve even created a few for testing. The prompt comes back within a second, even though there are upwards of 800 000+ files/folders in this drive. I am the sysadmin for the company and have full admin rights on all drives.

Any ideas?

(Win 7 Pro 64 bit)

11 Steve Wiseman June 8, 2012 at 9:55 am

Try it with the -l and -v options…do you get any output?

This should list what the program can see.

Thanks,

Steve

12 Patrick July 3, 2012 at 9:01 am

No, still get the “Nothing found to delete” message instantly. My syntax was:

delempty.exe -d -s -l -v h:\

Regards

Patrick

13 Randy August 23, 2012 at 10:47 am

I can’t get this to open at all. I know how to run a command prompt, but when I double click this it just flashes real fast. I am running Windows 7 (x64) HP. Could you give me a heads up on starting this. Please and thank you.

14 Steve Wiseman August 23, 2012 at 1:26 pm

Hi Randy,

This is a command line program. You will need to get to a command prompt first, and then call delempty from there.

Please be careful as this is a deletion tool…make sure you use the testing options first to see what it will delete.

Steve

15 Niel November 28, 2012 at 5:06 pm

Hi Steve,

Thanks for the tool!

I am having the same problem as Patrick on a non-network drive. I’ve tried it from the root of the drive and otherwise. I am also running as an admin.

Any advice?

16 Rudi April 25, 2013 at 12:29 pm

Same pb as Patrick. I even created a simple short named zero byte file. Message : Nothing found to Delete

17 Justin May 27, 2013 at 2:59 pm

Not sure if it’s a feature or a bug but I also noticed that it did not work when you only use “e:\”

I like to think of it as a feature since my finger just slipped and hit the enter key right after I hit the “\” key. Just about freaked out thinking that I erased all the empty folders on that drive. Not that it would have hurt much but I need some of those empty directories.

Also, I tried the -l option with a space after the “e:\” (so that it would be “e:\ “)and it found all the empty directories on the drive.

18 john October 9, 2013 at 8:48 pm

This is a great utility.Could you please help by giving me a batch file command to automate the process? Thank You!

19 Mike November 8, 2015 at 6:39 pm

Thanks. Worked for me, but noticed the tool wouldn’t work on root folders.

Example

D:\test1\
D:\test1\test2\
d:\test3\

delempty.exe -d d:\

^ does not work

20 Ralph February 10, 2016 at 11:52 pm

We are using a system that creates PDF files, and every now and then, it creates a zero byte PDF file. When it does, the UNIX system that attempts to read that file, breaks down, the Unix administrator on that side was unable or unwilling to create a simple script to delete those zero byte files. Your DelEmpty.exe was a brilliant, practical and most efficient solution. Thank you.

Leave a Comment

Category Links - Windows Forum - Exchange Forum