oops, use "Suppliers" instead of "folders" in line 3 :
cd c:\Suppliers
Main Topics
Browse All TopicsW2k, I need a batch file which will run through a folder, and all it's subfolders, deleting every folder in the subfolders EXCEPT ones named "images". Right now, the top folder is called "Suppliers", and in it are a hundred folders all named diff things. In each of these folders is an "images" folder. I want to end up with all the diff named folders, each with thier "images" folder and nothing else in them. Thanks..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
ah, so that is where I was confused, because to me preserving the structure means not removing the folders.
In that case I re-submit this with changes to accomodate your needs:
@echo off
c:
cd c:\Suppliers
:first delete all files not in "images" folders
dir/s/b/ad | find/v "\images" > filelist.txt
for /f "delims=" %%F in (filelist.txt) do del/y "%%F\*.*"
:next, delete the empty folders
dir/s/b/ad > filelist.txt
:you will need to repeat the next line of code for the deepest number of folder-levels you have (I use 3)
:it will not remove non-empty folders like images
for /f %%D in (filelist.txt) do rd %%D > nul
for /f %%D in (filelist.txt) do rd %%D > nul
for /f %%D in (filelist.txt) do rd %%D > nul
also, I forgot the quotes around the rd statements, so I'm re-submitting with corrections:
@echo off
c:
cd c:\Suppliers
:first delete all files not in "images" folders
dir/s/b/ad | find/v "\images" > filelist.txt
for /f "delims=" %%F in (filelist.txt) do del/q "%%F\*.*"
:next, delete the empty folders
dir/s/b/ad > filelist.txt
:you will need to repeat the next line of code for the deepest number of folder-levels you have (I use 3)
:it will not remove non-empty folders like images
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
It is as long as the image folders don't have sub-folders under them. If so, then simply remove this line from my previous post:
dir/s/b/ad > filelist.txt
That will allow the script to use the previous version of filelist.txt, which does not contain any entrys for Images folders.
Also, you may want to add this line at the end, just to clean up:
del filelist.txt
hmm, my last post was confusion, let me re-state: As long as the Images folders do not contain sub-folders under them, then this should work:
@echo off
c:
cd c:\Suppliers
:first delete all files not in "images" folders
dir/s/b/ad | find/v "\images" > filelist.txt
for /f "delims=" %%F in (filelist.txt) do del/q "%%F\*.*"
:next, delete the empty folders
REM dir/s/b/ad > filelist.txt
:you will need to repeat the next line of code for the deepest number of folder-levels you have (I use 3)
:it will not remove non-empty folders like images
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
for /f "delims=" %%D in (filelist.txt) do rd "%%D" > nul
:cleanup
del filelist.txt
Business Accounts
Answer for Membership
by: knightEknightPosted on 2008-02-26 at 20:01:44ID: 20991255
@echo off
c:
cd c:\folder
dir/s/b/ad | find/v "\images" > filelist.txt
for /f "delims=" %%F in (filelist.txt) do del/y "%%F\*.*"