0

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?

2
  • del /s /q *.* should do it, but not a programming question Commented Dec 11, 2011 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
    Commented Dec 11, 2011 at 1:57

3 Answers 3

1
(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.

1
  • The first line does exactly what I needed. Thanks!
    – merlin2011
    Commented Dec 12, 2011 at 2:17
4

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

1

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

2
  • That gets pretty close, but it leaves more than the top-level subdirectories.
    – merlin2011
    Commented Dec 11, 2011 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
    Commented Dec 11, 2011 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .