Batch File - Delete Directory Contents

I want to make a batch file that basically does a rmdir.  However I want only the directories contents and subdirectories to be deleted, not the main directory itself.  Does any one know if this is possible, and if so how to do it?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:

@echo off

for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

Run this from the directory where you want to delete subdirecties. Note, I've placed an echo prior to the remove directory (rd) command for testing. To actually do the processing you'll want to remove the echo statement.

Good Luck,

del *.* /s /q

will delete all files and all subdirectories of the current directory, but not the directory itself.
Only problem is that command won't remove the subdirectories.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

oops ... you're right - that was submitted too fast.
Zeman4323Author Commented:
hey thanks for the imput.  I cant try it tonight, but I will tomorrow and Ill give you guys some feedback.  THanks Again
Zeman4323Author Commented:
When I try doing this:
for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

I get %%a unexpected at this time.

Sorry, but I was wondering if you knew what is going wrong.  

Also, Sorry for the trouble, and that it took me so long to get back to you.  I had a busy weekend.  However, I did add 20 more points to this question.  I would add some more, but 20 is all I have left

Well, Thanks Again
Very easy - try with this batch.

You should create a DC.BAT batch file (DC=Delete contents).

This version can delete content of any folder. The Syntax is

dc c:\Folder1\folder2\folder3  (and only the contents of folder3 is to be deleted)

In this File you should  copy next commands:

cd %1
rd %1 /s /q
md %1
cd %1

The last Line is optional and you can avoid it

A simplified Version is

rd %1 /s /q
md %1

but you should be in root of destination folder.

You should be in c:\Folder1\Folder2, and you can type:

DC Folder3

This batchfile can both be run commandline with the foldername mentioned after filename (in quotation marks) or run without arguments to have it prompt you for the foldername.

I warn you that I have not tested it and claim no responsibility for whatever may come from using it. The responsibility is yours.

This is effectively zvonkodj's batchfile except prettier and with a few "just in case" features. I only noticed he came up with it after I wrote this file because I refreshed the original page (so we've both independently come up with the same solution). You need to run it in the root folder.

ECHO Please note that this program is intended to run from the folder outside the one you want to delete.
IF NOT "%~1"=="" SET CLNDIR=%~1
IF "%~1"=="" SET /P CLNDIR=Enter Directory Name:
ECHO Error: Directory "%CLNDIR%" does not exist.
The following section:


Should be:

Caudax: You posted your original code on 7/12 and zvonkodj and I created our code on 7/5. You were sitting on this page for 7 days and constructed the code...

Zeman4323: I didn't see your response regarding the problem running my code from the command line. To do this you must only use one percent. My code was meant to be run from a batch file (.bat):

for /f "delims=" %a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%a"

That will do the trick from the command line. But it's much easier to create a batch file named DeteleSubs.bat:

@echo off

for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

Actually, I posted my code on 7/1 --- just for the record...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zeman4323Author Commented:
sorry it took me so long, thanks a lot
SteveGTR: Don't accuse me of being an [expletive]; at any rate, I did not see his code until I refreshed the original page by which time I had already coded my own.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.