CMD command to remove subdirectories and files from a certain folder

adiemeer
adiemeer used Ask the Experts™
on
Hello,

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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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
setlocal
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 Advisor
Most Valuable Expert 2013

Commented:
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

Alternatively:
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.
IT / Software Engineering Consultant
Top Expert 2016
Commented:
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
popd

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

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

~bp
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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
popd

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 Consultant
Top Expert 2016

Commented:
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
  popd
)


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
    popd
  )
  exit /b

Open in new window

~bp

Author

Commented:
Thanks!
Luis DiazIT consultant

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial