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.

link|improve this question

55% accept rate
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
feedback

2 Answers

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.

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

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

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

Your Answer

 
or
required, but never shown

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