Delete ALL files/folders BUT file-extension? WIN7

flexy

Diamond Member
Sep 28, 2001
8,464
155
106
goal:

Simple batch file that deletes all files/folders in specific folder EXCEPT files with a certain file extension?

(Elegant solution preferred)
 
Last edited:

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Code:
cd xyz  
del *.abc

replace xyz with the folder path and abc with the file extension.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Here is a batch file for txt file types
Code:
set target="C:\Documents and Settings\username\My Documents"

if not exist %target% GOTO ERROR

cd %target%

if not exist temp2 md temp2
if not exist temp2 GOTO ERROR

copy *.txt temp2

echo Y | del *.*
cd temp2
copy *.txt ..\*.txt
echo Y | del *.*
cd ..
rd temp2

:ERROR

You have to change the txt in 3 places. Also, replace the path in the first line with your path. Make sure this is correct.

This will only work for one folder, not with a whole tree.
 
Last edited:

postmark

Senior member
May 17, 2011
307
0
0
goal:

Simple batch file that deletes all files/folders in specific folder EXCEPT files with a certain file extension?

(Elegant solution preferred)

You state files/folders, but do you want to traverse the tree down through folders and still delete files there based on the certain file extension? Or do you only want to delete any folder that exists plus all the non matching files?

This will delete all files except those specified by the first parameter passed recursively through folders:

Code:
@echo off
@ attrib +h +r %3\%1 /S
@ %2 %3 /Q /S
@ attrib -h -r %3\%1 /S
@ goto :EOF

To use call it like this: <name of bat file>.bat *.<extension to keep> del "<folder to delete from>"

here's an example to delete all but pdf files in c:\test

test.bat *.pdf del "c:\test"
 
Last edited:

flexy

Diamond Member
Sep 28, 2001
8,464
155
106
do you want to traverse the tree down through folders and still delete files there based on the certain file extension?"

It should delete ANYTHING (files, folders) except files of a certain file-type in the root folder. What's *in* the specific folders is not of interest, if it sees a folder the folder can be entirely deleted.
 

postmark

Senior member
May 17, 2011
307
0
0
It should delete ANYTHING (files, folders) except files of a certain file-type in the root folder. What's *in* the specific folders is not of interest, if it sees a folder the folder can be entirely deleted.

OK, well here's a version that will do that for you then:

Code:
@echo off
@ attrib +h +r %3\%1 /S
@ %2 %3 /Q /S
@ attrib -h -r %3\%1 /S
@ for /f "tokens=*" %%d in ('dir %3 /ad/b/s ^| sort /R') do rmdir "%%d" /s /q 2>nul || (echo Not empty: %%d)
@ goto :EOF
 

dentedskillet

Junior Member
Jan 5, 2015
2
0
0
Postmark, thank you for the great solution. I was wondering, if I wanted to use this, but wanted it to exclude more than one extension, could you tell me how to do so? Thanks

You state files/folders, but do you want to traverse the tree down through folders and still delete files there based on the certain file extension? Or do you only want to delete any folder that exists plus all the non matching files?

This will delete all files except those specified by the first parameter passed recursively through folders:

Code:
@echo off
@ attrib +h +r %3\%1 /S
@ %2 %3 /Q /S
@ attrib -h -r %3\%1 /S
@ goto :EOF
To use call it like this: <name of bat file>.bat *.<extension to keep> del "<folder to delete from>"

here's an example to delete all but pdf files in c:\test

test.bat *.pdf del "c:\test"
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,599
4,511
75
Obviously to use this solution with multiple extensions, you'd want to run both attrib commands for each extension you want to exclude. How you'd want to pass that into a batch file I'm not sure.
 

mrjminer

Platinum Member
Dec 2, 2005
2,739
16
76
Postmark, thank you for the great solution. I was wondering, if I wanted to use this, but wanted it to exclude more than one extension, could you tell me how to do so? Thanks

You could pass it into the batch file using parameters when you run your .bat, or by loading a separate .bat inside of the executing .bat which just declares some variables (but you would sacrifice using local variables only using this particular method). Then, you'd have to parse the extension off each and do another "for" loop with the extensions you loaded (easiest if you just declared an array of extensions), and check whatever text is to the right of the "." farthest right against your "safe" extension array values.

Was just toying around with some batch stuff a couple of months ago, but it's beyond my skill to actually do this without a couple of hours of screwing around, so GL
 

dentedskillet

Junior Member
Jan 5, 2015
2
0
0
Thank you guys who responded. Unfortunately, my skill at writing batch files/commands is very minimal and I admit the suggestions given is over my head. I will continue to play with it and try to figure it out, but I admit it hasn't been very promising thus far.