Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that runs the command:

del c:\mydir\*.*

Is there a command line switch I can use that will also delete all subfolders in that directory? Thanks!

share|improve this question
up vote 6 down vote accepted

If there are files in the C:\mydir directory then you'll need to do both lines. Otherwise, the first line will do what you want:

FOR /D %i IN (C:\mydir\*) DO RD /S /Q "%i"
DEL /Q C:\mydir\*.*

That preserves the C:\mydir directory.

Edit: David1235 is quite right. If you want to do this in a batch file, you'll need to double-up the "%" in the "FOR ..." line.

It's a little unclear to me why David1235's script needs the "pushd" and "popd" when you can specify the path right in the "FOR ..." and "DEL ..." lines, though.

share|improve this answer
When I run the first line directly in the command prompt, it works. However, when I copy and paste it into a batch file, I get "\mydir* was unexpected at this time." – Mike Cole Dec 3 '09 at 17:27
Cancel that... I used the double percent signs and mentioned below. Thanks!! – Mike Cole Dec 3 '09 at 17:30

If you want to run your script from anywhere, try

@echo off
pushd "C:\mydir"
for /d %%d in (*.*) do rmdir /s /q "%%d"
del /q *.*

In a batch script, you need the double percent signs. From "help for":

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.
share|improve this answer
Thanks for the hint! I'd give you an upvote but don't have enough rep yet. – Mike Cole Dec 3 '09 at 17:31
+1 - I'll throw one your way since I didn't think to mention the double-% behaviour in my posting. – Evan Anderson Dec 3 '09 at 20:21

Is there a reason that:

DEL /Q /S C:\mydir\*

won't work?

share|improve this answer
Because it doesn't delete subfolders. – Mike Cole Dec 3 '09 at 17:17

Instead of complex FOR...LOOPS etc. I would just use:

del /q *.* /s 

... but an even faster method is:

rd /q /s c:\mydir

as RD is "remove directory" just as the old DELTREE

share|improve this answer – BerggreenDK Dec 29 '10 at 7:41

Yes, rmdir.

(hint: rmdir /?)

Edit: You probably want to keep the folder. Try this out in a batch file:

@echo off
rmdir C:\mydir /S
mkdir C:\mydir
share|improve this answer
Yes. but I don't want to remove the C:\mydir\ directory itself... just everything underneath. – Mike Cole Dec 3 '09 at 15:30
Re-edited my post, does this help? – pauska Dec 3 '09 at 15:31
I don't want to delete the folder at all. I have special settings/permissions etc set up on it and I don't want to have to recreate those. – Mike Cole Dec 3 '09 at 15:31
Ah. Then you'll need some more advanced batch scripting, wich I'm not so good at. Others will probably post something :) – pauska Dec 3 '09 at 15:35

i'm using virutal machine with ms dos 6.22. rd command can't have parameters...

to delete c:\folder\folder\folder with files and whatever if you are on c:\ use:

deltree \folder (it will ask for confirmation) deltree \y c:\folder (it will do it right away)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.