CMD command to remove subdirectories and files from a certain folder


I want to use the command prompt to empty all the files and subdirectory of a certain directory. For example the directory C:\test. Important is that I don't want to delete the C:\test, only the files and subdirectories in this folder. I tried the command:

del C:\test /q /s

But this command doesn't delete the subdirectories. The /q is important as I don't want to confirm that I really want to delete the files and subdirectories. Anyone idea how to solve this?

Regards Arne
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.

If you want to keep the root folder, you need two steps: delete all files in the folder, and delete all subfolders of the folder.
Handle with care.
@echo off
set Root=C:\Test
if not exist "%Root%" (
	echo Root folder '%Root%' not found.
	exit /b 1
del "%Root%\*.*" /f /q
for /d %%a in ("%Root%\*.*") do (
	rd /s /q "%%a"

Open in new window

Lee W, MVPTechnology and Business Process AdvisorCommented:
First point - why does this have to be ONE command?

Second point - why not just delete test completely and then recreate it a moment later?

RD /q /s c:\test & md c:\test

del c:\test\*.* /q & for /f %a in ('dir c:\test /ad /b') do rd /s /q c:\test\%a

I assume you're NOT putting this in a batch file - if you are, one of the above won't work without modification.
Bill PrewIT / Software Engineering ConsultantCommented:
Here's an approach that works, and simplifies things a bit.  By switching to the directory you want to purge but preserve, it becomes the current directory and therefore can't be deleted.  Then when you are done purging pop back to whatever the default directory was before.  I typically do this as:

pushd c:\temp
rd /q /s . 2>NUL

I think you could also do it on "one line" as:

pushd c:\temp && (rd /q /s . 2>NUL & popd)


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
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Sorry, BillPrew, but that first solution requires a warning (directed at everyone, not you specifically):

[b]Do never, ever, combine cd/pushd and delete/rd without verifying that the cd/pushd command worked![/b]

If a "del W:\herever" fails because W:\herever doesn't exist, all you get is an error message. No harm done.
If a "pushd W:\herever&rd /q /s ." fails, the current folder and all its subfolders will be deleted. Potentially lots of harm done. Imagine a scheduled task that by default starts in System32, and a network drive (stranger things have happened).

So you'll end up either with something like
pushd c:\temp
if errorlevel 1 goto :eof
rd /q /s . 2>NUL

Open in new window

or with your second solution, which checks the pushd errorlevel.
Leaves the issue that the 2>NUL suppresses not only the failed root folder, but everything else as well.
Bill PrewIT / Software Engineering ConsultantCommented:
Thanks oBdA, good points.

So I guess I'll propose these three approaches built off of the CD trick I started from.  But certainly brute force without the PUSHD/POPD works great too, just a bit more code.  But maybe wrap it in a funtion if needed often.

rem Inline, verbose method
set DirName=c:\temp
pushd "%DirName%" && (
  rd /q /s "%DirName%" 2>NUL

rem Inline, compact method
pushd "c:\temp" && (rd /q /s "c:\temp" 2>NUL & popd)

rem Function call
call :PurgeDir "c:\temp"
exit /b

:PurgeDir [directory-path]
  pushd "%~1" && (
    rd /q /s "%~1" 2>NUL
  exit /b

Open in new window

adiemeerAuthor Commented:
Based on last experts feedback exchange between Bill Prew and oBdA the assisted solution should be the last comment of Bill Prew and not the one reported previously. Can someone repopen the question and assign the right assisted solution. This will allows us to add the right solution to our personal Knowledge base.
Thank you very much for your help.
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.