# Deleting subdirectories in DOS



## congabeatz (Apr 8, 2011)

Hello-

I'm trying to delete a number of subdirectories (and any files/directories that might be in that subdirectory) that have a certain string in the directory name. If I have a directory C:\MainDirectory\ and I want to delete subdirectories that have the string 'Subdir', how do I accomplish this?

For example, assume I have the following directories:

C:\MainDirectory\Subdir1
C:\MainDirectory\Subdir2
C:\MainDirectory\SomeOtherDirectory\Subdir3

Let's say I want to delete the directories named Subdir1, Subdir2, and Subdir3. How is this done? I've tried various RMDIR commands with no luck. Does this need to be run recursively?
Thanks


----------



## Couriant (Mar 26, 2002)

RMDIR removes the directory *if* it is empty. The best command would be *rmdir /s [path of directory]*. However there is no command for it to search for directories and delete. You will need to enter each one in. For example:

*RMDIR C:\MainDirectory\Subdir1 C:\MainDirectory\Subdir2 C:\MainDirectory\SomeOtherDirectory\Subdir3
*

or

*RMDIR Subdir1 Subdir2 \SomeOtherDirectory\Subdir3
* if you are already in the C:\MainDirectory directory


----------



## Ent (Apr 11, 2009)

As noted RMDIR /s can be used to kill full folders. I'd use a batch script with the 
for /R /D 
command to navigate through all the folders in Maindirectory.

This one should navigate through the current folder and all subdirectories. It will remove any folder starting subdir.

```
@echo off
for /R /D %%F in ([COLOR=Red]subdir*[/COLOR]) do (
rmdir "%%F" /S /Q
)
Pause
```
Remember that to create a batch file, 
Open Notepad
Copy and paste everything in the code box
Click Save as
Save as NoSubDir.bat
The .bat extension is most important.
Doubleclick to run. It will run against the folder it's in.

Please test it does what you want in a folder with fake files before actually setting it against your real machine.


----------



## congabeatz (Apr 8, 2011)

Thanks for the responses. I'll have to try these out!


----------

