Super User is a question and answer site for computer enthusiasts and power users. 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

Need to empty a folder in Windows 7 from the command prompt or batch file. This means deleting all the files and all sub folders and leaving the empty folder. del /s /q leaves empty subfolders so this solution doesn't work for me. I don't want to delete and recreate the folder either.

share|improve this question
Can you exlpain why there is the condition to not delete/recreate the folder? Otherwise rmdir /s /q %dir followed by mkdir %dir is the trivial solution. – Mike Fitzpatrick Oct 11 '10 at 2:55
@Mike: I'm guessing, timestamps. – paradroid Oct 11 '10 at 3:04
Because there might be files in use and the folder won't get deleted. Then I have to add more code to create the folder only if it doesn't exist. There's a command to cleanly empty a folder. I don't remember it. – Tony_Henrich Oct 11 '10 at 3:27
up vote 4 down vote accepted

You can use the sdelete (Secure delete) command to clean a folder.

sdelete -s *

from with in said folder to clear all of the contents.

share|improve this answer
Works good although I had to use the p option because it still left an empty folder. – Tony_Henrich Oct 11 '10 at 3:57
Odd as -p 1 is the default. – steve.lippert Oct 11 '10 at 18:01

Try the RMDIR (or aka the RD) command.

RMDIR [/S] [/Q] [drive:]path

RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

share|improve this answer
I think the OPs issue is that he wants to only delete the contained directories and files, while keeping the top level directory. RD does not support wildcards like DEL does. – paradroid Oct 11 '10 at 3:09

In a batch file:

for /D %%p IN (*) do rmdir /S /Q %%p
for %%p in (*) do del %%p
share|improve this answer

You must log in to answer this question.

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