SS64 Discussion Forum

You are not logged in.

#1 2009-10-28 20:32:00

NDog
PDP-11
From: New Zealand
Registered: 2006-05-31
Posts: 70
Website

delete empty sub-directories

I am not sure how to detect if a directory is empty, ie there are no files inside. If I try something like - if not exist *.* echo empty - does not work.

What I am trying to do is delete empty sub-directories, firstly by scanning every sub-folder and deleting empty sub-directories and then working its way back up and if it is now empty, deleting that, until it gets to the original folder.

For example

c:\test\folder to process

contains 3 folders and their subdirectories

folder 1\empty folder
folder 2\not empty folder\not empty folder\empty folder
folder 3\empty folder\empty folder

In this scenario the script would scan folder 1\empty folder and then delete the empty folder, and if no other files exist in folder 1, delete folder 1

It would also scan folder 2\not empty folder\not empty folder\empty folder, and determine that the empty folder is empty remove it, and work back finding folder 2\not empty folder\not empty folder is not empty so continue working

I think you get the idea, transverse a target folder and remove all empty sub-directories. I would have had a try at sloving this already, but my problem is I am not sure how to check for empty directories, so I cannot start on this one.

Any ideas?


Known Scripting Languages: CMD, Autoit, 4DOS

Offline

#2 2009-10-29 05:35:01

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 702

Re: delete empty sub-directories

This is what I'd do:

@echo off

set "cleanup=c:\test\folder to process"

for /f "usebackq tokens=*" %%a in (`dir /b/s/ad "%cleanup%" ^| sort /r`) do (rmdir "%%~a" 2>nul && echo:Removed: "%%a")

pause

This will attempt to remove all directories, starting at the lowest level.  For any directories that aren't empty, the "rmdir" command will fail, but the failure message will be hidden.  It's much easier than trying to figure out if the directory actually has any files in it.  NB -- If it's a large directory structure, it make take a while to scan the contents before it starts to do anything.

Offline

#3 2009-11-05 22:05:13

NDog
PDP-11
From: New Zealand
Registered: 2006-05-31
Posts: 70
Website

Re: delete empty sub-directories

Thanks for the reply. I used this script succesfully to remove empty subfolders. At first I though using rmdir on all folders was unsafe but I realise rmdir wont remove a folder unless its empty. Quite an interesting approach, i thought it would involve a lot of heavy parsing like dir*.* | find and multiple sub routine calls, but this simple approach works like a charm. Thanks


Known Scripting Languages: CMD, Autoit, 4DOS

Offline

#4 2010-02-04 23:39:02

Jason404
New Shoes
From: London, UK
Registered: 2009-04-23
Posts: 6

Re: delete empty sub-directories

@bluesxman

In an attempt to figure out the IF command (which I have been struggling with for a long time), I tried to change your script to just list empty directories.

@echo off

set "cleanup=%1"

for /f "usebackq tokens=*" %%a in (`dir /b /s /a:d "%cleanup%" ^| sort /r`) do (echo "%%~a" 2>nul && echo:Empty: "%%a")

It lists all the directories, but I can see that happens because the ECHO command does not fail.

So that I can begin to understand this, could you please explain what these mean?:

"usebackq ..."= ... (` ... `)   - what is the point of this?

%%~a   - what does the tilde do?

&&   - I think this means AND, but I cannot find the page on this site where it explains it.

And what would be the best way to figure out if a directory is empty of not?

Cheers

Last edited by Jason404 (2010-02-04 23:46:46)

Offline

#5 2010-02-05 05:23:16

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 702

Re: delete empty sub-directories

Using the "usebackq" option modifies the syntax for the "for /f" command.

If you do "for /?" the built in help makes a decent fist of explaining "usebackq", the back-ticks and the function of the tilde.

In this context, "usebackq" is not necessary; I just use prefer the syntax modifications it implies, so it ends up in all my scripts out of habit (aside from the rare occasion where I specifically need to use the unmodified syntax).  The tilde is not required here either, it's another habitual thing for me.

&& is explained here:

http://ss64.com/nt/syntax-conditional.html

There's no ideal way to determine if a directory is empty with pure CMD, but there are a few ways to do it.  Here are some examples using different methods

dir /b "x:\some directory\" | findstr "." >nul && echo has files || echo is empty
set empty=1
for %%a in ("x:\some directory\*") do set empty=
if defined empty (echo is empty) ELSE (echo not empty)

The above methods have the potential to be slow if there are lots of files.  This one uses the same basic method as the previous example but it will be much quicker:

call :check.empty "x:\some directory"

if not errorlevel 1 (echo is empty) ELSE (echo has files)

pause

goto :EOF

:check.empty

for %%a in ("%~1\*") do exit /b 1

exit /b 0

Offline

Board footer

Powered by FluxBB