# Solved: DOS Batch File to Delete Certain Folders Based on Name



## theBorja (Nov 29, 2012)

I am going to reference this previous post as it is close to what I am trying to do but not quite.

http://forums.techguy.org/dos-other/1035509-dos-batch-file-delete-certain.html

I am attempting to create a .bat file that will delete folders with specific characters but leave other folders. I plan on scheduling this to run on a certain date that I will do with task scheduler.

The sub-folders I need removed always contain fic_ at the beginning and may contain random characters after the _ . The other sub-folders need to remain in the directory

This is the code I am using that is not working. I am running this on Windows 2003 R2

@echo off
pushd "C:\TempDir\"
for /F "tokens=*" %%I in ('dir /b ^| findstr /B /I /C:"fic"') do echo rmdir /q /s "%%I"
popd


----------



## Squashman (Apr 4, 2003)

Not going to remove anything with the ECHO cmd in there. You should change your DIR command to only list Directories to be on the safe side.


----------



## Squashman (Apr 4, 2003)

You could also look at using the FOR /D option as well. That simplifies it a lot.


----------



## foxidrive (Oct 20, 2012)

Here's a modification including squashman's comment re checking just folders: (untested)


```
@echo off
pushd "C:\TempDir\"
for /F "delims=" %%I in ('dir /ad /b ^| findstr /r /i "^fic_"') do echo rmdir /q /s "%%I"
popd
pause
```
I expect that the echo is for testing purposes, but you wanted to keep the folders themselves, right? Does that include a folder tree inside the ones that you are removing?

say there is a 
C:\TempDir\fic_123
and a
C:\TempDir\fic_123\xyz

do you want to keep those two folders, but erase all the files?


----------



## theBorja (Nov 29, 2012)

No I do want to remove the folders and the files within, but only the folders with "fic" (fic_123 & fic_xyz for example) A process creates the folders and that same process is supposed to overwrite whats ever in there, but that's not working.


----------



## theBorja (Nov 29, 2012)

FoxDrive, 
I've tested your code without the echo line, and it does list the directories I want to delete, but does not delete them or anything inside them. I feel like I'm close, but something's missing. 

Squashman, I attempted your suggestion using For /D, and that didn't help. 

any further input is greatly appreciated.


----------



## foxidrive (Oct 20, 2012)

theBorja said:


> FoxDrive,
> I've tested your code without the echo line, and it does list the directories I want to delete, but does not delete them or anything inside them. I feel like I'm close, but something's missing.


What is the error message on the screen? If you removed the 'echo' then it's going to try and remove the folders - and if it doesn't then there will be a hint why it didn't, on the screen.


----------



## Squashman (Apr 4, 2003)

theBorja said:


> Squashman, I attempted your suggestion using For /D, and that didn't help.
> 
> any further input is greatly appreciated.


You really need to be a little more forthcoming with your input. Telling me something didn't help doesn't mean anything. I have no idea how you tried to implement it. You need to start posting the code you are attempting to use and show us the output of the code or any error messages it is giving you.


----------



## Squashman (Apr 4, 2003)

Foxidrive's code works just fine.

```
C:\batch>dir /ad c:\tempdir
 Volume in drive C has no label.
 Volume Serial Number is 789C-4662

 Directory of c:\tempdir

11/30/2012  05:41 PM    <DIR>          .
11/30/2012  05:41 PM    <DIR>          ..
11/30/2012  05:41 PM    <DIR>          FIC_123
11/30/2012  05:41 PM    <DIR>          Fic_XYX
               0 File(s)              0 bytes
               4 Dir(s)  208,195,923,968 bytes free

C:\batch>DelFicFolders.bat

C:\batch>dir /ad c:\tempdir
 Volume in drive C has no label.
 Volume Serial Number is 789C-4662

 Directory of c:\tempdir

11/30/2012  05:42 PM    <DIR>          .
11/30/2012  05:42 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  208,195,923,968 bytes free

C:\batch>
```


----------



## Squashman (Apr 4, 2003)

And using FOR /D also works.

```
@echo off
pushd "C:\TempDir\"
for /D %%I in (fic*) do rmdir /q /s "%%I"
popd
```
Output

```
C:\batch>dir /ad c:\tempdir
 Volume in drive C has no label.
 Volume Serial Number is 789C-4662

 Directory of c:\tempdir

11/30/2012  05:46 PM    <DIR>          .
11/30/2012  05:46 PM    <DIR>          ..
11/30/2012  05:45 PM    <DIR>          FIC_123
11/30/2012  05:46 PM    <DIR>          fic_xyz
               0 File(s)              0 bytes
               4 Dir(s)  208,192,843,776 bytes free

C:\batch>DelFicFolders2.bat

C:\batch>dir /ad c:\tempdir
 Volume in drive C has no label.
 Volume Serial Number is 789C-4662

 Directory of c:\tempdir

11/30/2012  05:47 PM    <DIR>          .
11/30/2012  05:47 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  208,192,708,608 bytes free

C:\batch>
```


----------



## theBorja (Nov 29, 2012)

Thanks for the help. I realized my mistake and missed the echo line in the statement. I have been able to get the command to execute accordingly. 

thank you again.


----------



## Squashman (Apr 4, 2003)

That was my first response! And Foxidrive commented on my response. And the thread you got the code from I also stated the same thing about removing the echo. Reading is FUNdamental.


----------

