DOS - How to delete all files and subdirectories in a directory.

By CharlieSpencer ·
Okay, not really DOS, but from a Windows Server command prompt.

I'm trying to remember how to delete all files, subdirectories, and subdirectory contents from a specific directory from a command prompt. We have a "catch all" folder for short-term file sharing and I want to delete the contents nightly, but not the folder itself, using a scheduled command procedure.

Unfortunately I'm the poster boy for the American CRS Foundation and have forgotten how to do this. RMDIR won't take a wildcard. DELTREE is apparently dead and gone. I'm currently using

DEL /F /Q /S C:\Directory\*.*

but that leaves the empty subdirs in place.

Help. Thanks.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -


by scott_heath In reply to DOS - How to delete all f ...

Not really, but it sounded like a good idea.

Anyway, type "rd <path> /s /q"

Enjoy the nachos.

Collapse -

Unfortunately, the path changes regularly.

by CharlieSpencer In reply to CAUTION!!

The parent folder is used for temporary storage for files too big for e-mail attachments, holding scanned images before they're entered into our document management system, etc. Subdirectories with various names are created throughout the day. Those names are not the same from one day to the next, so I can't predict what the path will be. One day it may be


the next day




I want to leave C:\Directory each night but lose the subs.

Collapse -

This is not the best idea...

by scott_heath In reply to Unfortunately, the path c ...

But I'm going to offer it anyway!!!

REM ***** My Crazy Batch File
dir *. /b > deleteme.txt
for /f %%a in (deleteme.txt) do (
rd %%a
del deleteme.txt
REM ***** Muhahahahahaha

So anyway, as long as the directory doesn't have a .anything on the end of it your good. I realize this is stupid, but trust me I can get stoopiderer. I can show you a vbscript that uses the folders collection of the Scripting.FileSystemObject to do this as well. Or we can get totally crunked and use WMI to do this!!!

P.S. - I know I used the word "crunked" wrong, but I've been dying to use it.

P.P.S - I think I've had to much sugar today.

P.P.P.S - Eat nachos.

Collapse -

This may help

by Tig2 In reply to DOS - How to delete all f ...

I cheated. I Googled. I am also a CRS poster child...

Remove a Directory and All Subdirectories

SUMMARY: Use these Windows XP and DOS command line parameters with the RMDIR command to remove all subdirectories.

The Windows XP DOS command RMDIR removes a directory from your system and has the following syntax:

RMDIR directory_name

For example, to remove directory C:\BLAH, enter:


Note that you cannot be inside the directory that you are deleting; else you will be prompted with the message "The process cannot access the file because it is being used by another process".

Normally the RMDIR command will not remove a directory if it is empty. However, if you add the /S parameter, all files and directories underneath the specified directory will be removed. USE WITH CAUTION! You will be prompted with an "Are you sure" question that lets you back out in case you entered the wrong directory name.

If you need to perform this task in a batch file, you can add the /Q parameter to the end. This will delete the specified directory, all files, and all subdirectories, WITHOUT ANY WARNING.

For example, the following command will remove directory C:\blah and all subdirectories and files contained therein. No prompt will be displayed.

RMDIR c:\blah /s /q

If you wish you can use RD as an abbreviation for RMDIR.

Collapse -

Tig, see my reply to scott_heath

by CharlieSpencer In reply to This may help

The subdirectory names change daily.

Collapse -

Then add the MD

by IC-IT In reply to Tig, see my reply to scot ...

to the end of the script.

rd c:\directory\ /s /q
md c:\directory

Collapse -

Okay, that's getting closer

by CharlieSpencer In reply to Then add the MD

How do I assign the correct security settings to the new directory? I'm talking NT security, not share security. I'm looking through the help for the NET command, but that doesn't seem to be it.

Collapse -


by IC-IT In reply to Okay, that's getting clos ...

Add that and tweak to the needed security settings.

Collapse -

Could you change the procedure/location....

by ThumbsUp2 In reply to Tig, see my reply to scot ...

Is it possible to create another level of subdir that you can delete nightly?

For example, you have c:\directory which you want to leave and just delete what's under it, and that changes on a daily basis. Can you create c:\directory\temp and have everyone put their "stuff" inside of temp which can be deleted nightly and recreated new every day? You could then use an if statement to see if temp exists, if it does, deltree c:\directory\temp *.* ... then an if statement to check that it does not exist and if not, recreate it new.

Collapse -

Please see my reply to bwilmot.

by CharlieSpencer In reply to Could you change the proc ...

That's similar to his suggestion, but there's still the issue of how to recreate the security settings.

Also, the downside to this is all of our network scanners are programmed to use C:\Directory. If I get desperate, I could reset them to C:\Directory\Temp. And your way the security might not be an issue since it would pick up the settings from the parent C:\Directory. The problem then becomes one of educating users to use \Temp and not \Directory; I don't know if they'll bother, and can't see how to enforce it. There's no way to lock them out of \Directory without having different security settings than \Temp, and then we're back where we started.

Related Discussions

Related Forums