Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Andrius
- Posts: 37
- Joined: 26 Jun 2012 15:15
#1
Post
by Andrius » 19 Jul 2012 11:38
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?
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 19 Jul 2012 11:47
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"
)
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 19 Jul 2012 12:13
Andrius wrote:Wow you work fast! Thanks!
Easy code. If it was a college class it would be called: FOR LOOP 101
-
alan_b
- Expert
- Posts: 357
- Joined: 04 Oct 2008 09:49
#5
Post
by alan_b » 19 Jul 2012 13:05
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
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#7
Post
by Squashman » 19 Jul 2012 13:09
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.