For example:

I'm in the directory:

F:\Data

Inside this directory, I have four directories:

F:\Data>dir
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?

link|improve this question
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
feedback

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

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

2 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.

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

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

link|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
feedback

Your Answer

 
or
required, but never shown

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