Take the 2-minute tour ×

For example:

I'm in the directory:


Inside this directory, I have four directories:

22179 22915 23459 23460

These directories have various content, including directories and files. I'm trying to run something like:

rmdir /s *\*

where I delete all the contents of these numbered directories, while leaving the empty directories. Is there a one-liner that can do this, or do I have to loop through the sub-directories?

share|improve this question

migrated from stackoverflow.com Dec 11 '11 at 11:48

This question came from our site for professional and enthusiast programmers.

del /s /q *.* should do it, but not a programming question –  Damien_The_Unbeliever Dec 11 '11 at 1:41
I do not actually know if what I'm asking is possible. I am seeking a clean simple, solution to what I feel should be a simple problem, but I don't know if the Windows command line can do it. –  merlin2011 Dec 11 '11 at 1:57
add comment

3 Answers

up vote 1 down vote accepted
(for /d %A in (*) do @for /d %B in ("%A\*") do @rd /s /q "%~B")&del /s /q *.* >nul

It's a one liner - but I wouldn't call it simple. It loops through the 1st two levels of the tree, but everything deeper will be removed without more looping. If in a batch file then %A and %B become %%A and %%B

I wasn't sure if you wanted all files deleted from your root (F:\data in your case). The above will delete files found in your root.

If you want to preserve the files in root, then I think this should work (I didn't test this one)

for /d %A in (*) do @(@for /d %B in ("%A\*") do @rd /s /q "%~B")&@del /q "%A\*.* >nul

The @ symbols can be removed from both sets of code - they are just there to prevent each level of the command from being echoed.

share|improve this answer
The first line does exactly what I needed. Thanks! –  merlin2011 Dec 12 '11 at 2:17
add comment

Try this I think this will help....

RD /s /q (and then the path to folder which is to be removed)

it will remove all the contents with the folder itself

share|improve this answer
add comment

Does DEL /S /Q *.* work for you?

share|improve this answer
That gets pretty close, but it leaves more than the top-level subdirectories. –  merlin2011 Dec 11 '11 at 1:55
That is, if I have a directory 22179\Docs, it doesn't remove that directory and I would like it to. –  merlin2011 Dec 11 '11 at 2:01
add comment

Your Answer


