# Solved: cmd to not delete certain files and subfolders



## jlogan_coconino (Oct 25, 2013)

Came across this site and read http://forums.techguy.org/dos-other/1035509-dos-batch-file-delete-certain.html. This was very helpful but what I am trying to accomplish is a little different. I need a cmd that will look at a directory and subdirectories and delete all jpeg files but not ones containing the word "reduced" in it and for it to not delete the subdirectory folders themselves. Here is what I have so far thanks to Squashman:

@echo off
pushd "c:\temp\pics_orig"
for /F "tokens=*" %%I in ('dir /a:d-s-h /b ^| findstr /I /V /S reduced') do rmdir /q /s

"%%I" popd

This works but also deletes the subdirectory folders. I need the subdirectory folders searched and subsequent files that do not contain the word 'reduced" deleted but I do not want the folders themselves deleted. What am I missing?

Thanks!

JARED


----------



## foxidrive (Oct 20, 2012)

Can you confirm that you want to recurse through a directory tree and delete all jpg files that do not contain the word "reduced" in them (case insensitive)?

This should echo all those files to the screen - remove the echo to actually delete the files after you have tested this.


```
@echo off
pushd "c:\temp\pics_orig"
for /F "delims=" %%a in ('dir *.jpg /a-d /b /s ^| findstr /I /V /R "^.*\\.*reduced.*" ') do echo del "%%a"
popd
```


----------



## jlogan_coconino (Oct 25, 2013)

Yes, I need the script to go through the parent directory and subdirectories in order to find and delete all jpg files with the exception of those with the word "reduced" in them. I'll give this a shot and let you know. Thanks!!


----------



## jlogan_coconino (Oct 25, 2013)

That did the trick! Thank you so much!

JARED


----------

