# Batch script to remove subfolders/files only



## planejane (Mar 17, 2011)

Hi, I'm looking for a batch script that will remove subfolders and files under a parent directory that will not remove the parent directory and preferably not remove other specified directories under that parent directory. 

I have an application that is causing my C: to fill up with junk folders under the %windir%\Temp folder. The app creates folders with non-standard names such as "4y78" or "12UY" or "HN9o". So the subfolders aren't exactly easy to remove based on a standard name. The creation dates are daily, so I could try a script that would remove subfolders/files based on creation date so long as it wouldn't wipe out my needed folders (such as log folders). I have other apps that write to the Temp folder for log files which get monitored daily, which is why I can't delete everything under the Temp directory. I have tested out many different scripts, but none seem to work exactly like I want. I've tried batch and .vbs scripts and had some success but not complete success. The most I have been able to get was deleting everything under Temp. I want to specify which directories under the Temp folder to keep and then wipe everything else out. 

Make sense?
Oh, the OS is on both Win2k3 and Win2k8 servers. The offending app.....(the dreaded) McAfee. (and yes, I have a support ticket into them) 

I'm on the verge of giving up and doing the removal manually, but it would be a daily removal and would be over the course of a couple of hundred machines. Not exactly easy. 

Thanks in advance for all the brainstorming!!


----------



## Squashman (Apr 4, 2003)

Easily done within a for loop using the dir command and findstr. If there are alot of folder you need to exclude you could put the folders you want to exclude from deletion into a single text file. One on each line. You can tell the findstr command to use the text file as its search parameters and also tell findstr to do a inverse match.


----------



## planejane (Mar 17, 2011)

Ah, thank you. I will give that a try!


----------

