Page 1 of 1

Remove everything except PNG

Posted: 19 Jul 2012 11:38
by Andrius
I need a file that essentially drills through every folder and deletes ALL files except for PNG extensions yet leaves folder structure intact.

Code: Select all

[b]Example:
[/b]C:\Example\delete_all_but_png.bat
C:\Example\test1.png
C:\Example\Another Folder\test1.psd
C:\Example\Another Folder\test1.exe
C:\Example\Another Folder\test1.bmp
C:\Example\Test Folder\test2.png

[b]Result would leave me with:[/b]
C:\Example\delete_all_but_png.bat
C:\Example\test1.png
C:\Example\Another Folder\ (EMPTY FOLDER)
C:\Example\Another Folder\ (EMPTY FOLDER)
C:\Example\Another Folder\ (EMPTY FOLDER)
C:\Example\Test Folder\test2.png


Thoughts?

Re: Remove everything except PNG

Posted: 19 Jul 2012 11:47
by Squashman

Code: Select all

@echo off
FOR /F "delims=" %%G IN ('dir /a-d /b /s ^|find /v "%~dpnx0"') do (
   IF /I NOT "%%~xG"==".png" del "%%~G"
)

Re: Remove everything except PNG

Posted: 19 Jul 2012 12:01
by Andrius
Wow you work fast! Thanks!

Re: Remove everything except PNG

Posted: 19 Jul 2012 12:13
by Squashman
Andrius wrote:Wow you work fast! Thanks!

Easy code. If it was a college class it would be called: FOR LOOP 101

Re: Remove everything except PNG

Posted: 19 Jul 2012 13:05
by alan_b
Andrius wrote:I need a file that essentially drills through every folder and deletes ALL files except for PNG extensions yet leaves folder structure intact.

Result would leave me with:
C:\Example\delete_all_but_png.bat
C:\Example\test1.png
... etc ...
Thoughts?


That is a little inconsistent.
delete_all_but_png.bat does not have a PNG extension so it fails to meet the spec.
Not that I expect a BAT file to be successful in deleting itself :)

Re: Remove everything except PNG

Posted: 19 Jul 2012 13:07
by Andrius
hahah good eye

Re: Remove everything except PNG

Posted: 19 Jul 2012 13:09
by Squashman
alan_b wrote:
Andrius wrote:I need a file that essentially drills through every folder and deletes ALL files except for PNG extensions yet leaves folder structure intact.

Result would leave me with:
C:\Example\delete_all_but_png.bat
C:\Example\test1.png
... etc ...
Thoughts?


That is a little inconsistent.
delete_all_but_png.bat does not have a PNG extension so it fails to meet the spec.
Not that I expect a BAT file to be successful in deleting itself :)

I looked at was his input was and what his expected output was. I saw he was going to put the batch file in a folder that had PNG files in it, which is why I threw in the PIPE to the FIND command.