5

The question is pretty much what is asked in the title.

I have a lot of PNG files created by MapTiler. 24083 files to be exact. They are within many folders which are in many folders i.e. a tree of folders, duh. Thing is, it's the biggest waste of time to manually PNGCrush all of those.

Does anyone have an algorithm to share for me please? One that could recursively crush all these PNGs?

I have a Windows PC and would love to have it rather in Java or PHP than another language (since I already know it well) But else something else might be fine.

Thanks!

wallyk
  • 56,922
  • 16
  • 83
  • 148
Pangolin
  • 7,284
  • 8
  • 53
  • 67
  • It's not *literally* impossible to manually PNGCrush all of them. It would take a long time, but that's not the same as it being literally impossible. – Jon Skeet Jun 29 '11 at 08:13

3 Answers3

13

You don't need anything special for this, just use the FOR command in the Windows Command Prompt.

Use this line:

FOR /R "yourdir" %f IN (*.png) DO pngcrush "%f" "%f.crushed.png"

The "yourdir" is the root-directory where the input files are stored.

The two %f's at the end:

  • The first one is the input filename
  • The second one is the output filename

-ow option added in 1.7.22 to make the operation in-place:

FOR /R "yourdir" %f IN (*.png) DO pngcrush -ow "%f"

See this page for more information of FOR.

Community
  • 1
  • 1
Gerco Dries
  • 6,682
  • 1
  • 26
  • 35
  • @Gerco, would this cover all the png's within multiple folders inside the main folder? (Recursion) – Pangolin Jun 29 '11 at 08:26
  • @Nideo: Yes, it's recursive and will process all png files in that directory and all below it. – Gerco Dries Jun 29 '11 at 08:28
  • @Gerco, can you please help me with setting pngcrush up for CMD usage? It tells me 'pngcrush' is nor recognized... What file must I copy into what folder to make that command exist? (I have no CMD experience, only using 'cd') – Pangolin Jun 29 '11 at 08:37
  • 9
    Above command was throwing error that it cannot overwrite existing file. I have modified it a bit and works now............................................................................................................................................ FOR /R "E:\workspace\pngs" %f IN (*.png) DO ( E:\workspace\pngcrush\pngcrush.exe -brute "%f" "%f-crushed.png" && move /Y "%f-crushed.png" "%f" ) – Varun Feb 16 '12 at 06:47
1

The program 'sweep' http://users.csc.calpoly.edu/~bfriesen/software/files/sweep32.zip lets you run the same command on all files in a directory recursively.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • How does sweep work? I can't 2-click on it (it just closes again) and it doesn't work if I drag the main folder into/over it. – Pangolin Jun 29 '11 at 08:25
  • It's a command line program, so you need to open `cmd` and type your stuff in. PNGCrush works from the command line too so you should be fine. – artbristol Jun 29 '11 at 08:29
  • Just checked, it might only run once per directory, so you might need to combine it with the other answer using `FOR` – artbristol Jun 29 '11 at 08:30
1

See: RecursiveIteratorIterator with RecursiveDirectoryIterator and exec (or similar)

With that you can use:

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('%your-top-directory%'));
foreach ($it as $entry) {
  if (strtolower($entry->getExtension()) == 'png') {
     // execute command here
  }
}
Yoshi
  • 54,081
  • 14
  • 89
  • 103