# Using a BAT to delete specific directories



## jdwright (May 3, 2012)

Good morning!

I routinely deal with servers (typically Windows 2003, some 2008) that are running out of disk space. My company has a policy in place that includes specific folders to delete in order to free up space. Included in that policy are $NTUninstallKB8$ folders used by Windows Update.

I'm trying to write a command that will delete all of these $NTUninstallKB*$ folders that are more than 90 days old. I've managed to write this script, which has been "frankensteined" together from different commands I've found in various places.


```
cd "C:\windows\"
for /F "tokens=*" %%I in ('dir /a:d /b ^| findstr /B /I /C:"$NTUninstallKB"') do echo rmdir /q /s "%%I" /d -90
```
As it's written right now, the command echos ALL folders that begin with the string '$NTUninstallKB', including folders which are newer than 90 days old and shouldn't be included.

Can someone help me see what I've done wrong?


----------



## Squashman (Apr 4, 2003)

Where did you find documentation that says the RMDIR command has a /D switch for days?

What you want to use is the FORFILES command.
http://technet.microsoft.com/en-us/library/cc755872(v=ws.10).aspx


----------



## jdwright (May 3, 2012)

Squashman said:


> Where did you find documentation that says the RMDIR command has a /D switch for days?


Well, I didn't. :-\ I was trying to build the command from examples I'd found in different places and was more or less hoping a /d switch would work for what I needed.

Thanks for the link. I'm trying to construct the command with forfiles now. Can you give me a suggestion as to how I should write the cmd that forfiles will execute on the folders it finds? RMDIR was my first thought but seeing as how it doesn't have a /D switch, I'm thinking it might not work.


----------



## Squashman (Apr 4, 2003)

You do realize that every command that you can use from the cmd shell has a help feature.
So if you would have actually typed: *rmdir /?* at a cmd prompt you would have seen that it doesn't have a /D switch.

There are plenty of examples on the MS link I gave you. Pretty much a dead give away as to which one you need to use.


----------

