0

I found this command on this site for recursively removing empty directories, but was unable to comment to ask this question since I just registered and have zero rep.......

for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"

I'm hesitant to run it on a server (worked great in a test directory on my Win7 laptop).....I ran it inside the directory I wanted it to start in. Should I just run it inside the directory it should recursively check or is there a way to set the start point?

2 Answers 2

1

To start the command in a different directory

for /f "delims=" %d in ('dir c:\pathtostart /s /b /ad ^| sort /r') do rd "%d"

and change c:\pathtostart as appropriate.


To use a batch file

Alternatively create a batch file RemoveEmptyDirs.cmd:

@echo off 
for /f "delims=" %%d in ('dir %1 /s /b /ad ^| sort /r') do rd "%%d"

and call as follows:

RemoveEmptyDirs c:\pathtostart

To test before removing

If you are hesitent then to test what will happen replace rd with echo. This will echo the list of directories that would be removed.

When you a happy with the list replace the echo with rd and run the deletion.

0
0

Use dir "%~1". This will handle quoted paths as well, in case you want to start in a directory which contains spaces (RemoveEmptyDirs "C:\program files").

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .