Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 311075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:53:30+00:00 2026-05-12T07:53:30+00:00

I wrote a batch file to use PngCrush to optimize a .png image when

  • 0

I wrote a batch file to use PngCrush to optimize a .png image when I drag and drop it onto the batch file.

In the what’s next section, I wrote about what I thought would be a good upgrade to the batch file.

My question is: is it possible to create a batch file like I did in the post, but capable of optimizing multiple images at once? Drag and drop multiple .png files on it? (and have the output be something like new.png, new(1).png, new(2).png, etc…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:53:31+00:00Added an answer on May 12, 2026 at 7:53 am

    Yes, of course this is possible. When dragging multiple files on a batch file you get the list of dropped files as a space-separated list. You can verify this with the simple following batch:

    @echo %*
    @pause
    

    Now you have two options:

    1. PngCrush can already handle multiple file names given to it on the command line. In this case all you’d have to do would be to pass %* to PngCrush instead of just %1 (as you probably do now):

      @pngcrush %*
      

      %* contains all arguments to the batch file, so this is a convenient way to pass all arguments to another program. Careful with files named like PngCrush options, though. UNIX geeks will know that problem 🙂

      After reading your post describing your technique, however, this won’t work properly as you are writing the compressed file to new.png. A bad idea if you’re handling multiple files at once as there can be only one new.png :-). But I just tried out that PngCrush handles multiple files just well, so if you don’t mind an in-place update of the files then putting

      @pngcrush -reduce -brute %*
      

      into your batch will do the job (following your original article).

    2. PngCrush will not handle multiple files or you want to write each image to a new file after compression. In this case you stick with your “one file at a time” routine but you loop over the input arguments. In this case, it’s easiest to just build a little loop and shift the arguments each time you process one:

      @echo off
      if [%1]==[] goto :eof
      :loop
      pngcrush -reduce -brute %1 "%~dpn1_new%~x1"
      shift
      if not [%1]==[] goto loop
      

      What we’re doing here is simple: First we skip the entire batch if it is run without arguments, then we define a label to jump to: loop. Inside we simply run PngCrush on the first argument, giving the compressed file a new name. You may want to read up on the path dissection syntax I used here in help call. Basically what I’m doing here is name the file exactly as before; I just stick “_new” to the end of the file name (before the extension). %~dpn1 expands to drive, path and file name (without extension), while %~x1 expands to the extension, including the dot.

      ETA: Eep, I just read your desired output with new.png, new(1).png, etc. In this case we don’t need any fancy path dissections but we have other problems to care about.

      The easiest way would probably be to just start a counter at 0 before we process the first file and increment it each time we process another one:

      @echo off
      if [%1]==[] goto :eof
      set n=0
      :loop
      if %n%==0 (
          pngcrush -reduce -brute %1 new.png
      ) else (
          pngcrush -reduce -brute %1 new^(%n%^).png
      )
      shift
      set /a n+=1
      if not [%1]==[] goto loop
      

      %n% is our counter here and we handle the case where n is 0 by writing the result to new.png, instead of new(0).png.

      This approach has problems, though. If there are already files named new.png or new(x).png then you will probably clobber them. Not nice. So we have to do something different and check whether we can actually use the file names:

      rem check for new.png
      if exist new.png (set n=1) else (set n=0 & goto loop)
      rem check for numbered new(x).png
      :checkloop
      if not exist new^(%n%^).png goto loop
      set /a n+=1
      goto checkloop
      

      The rest of the program stays the same, including the normal loop. But now we start at the first unused file name and avoid overwriting files that are already there.

    Feel free to adapt as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a batch file so I can use TortoiseHg Annotate
I have this simple little batch file program that I wrote but it fails
I am hoping to be able to use a simple batch file to be
We use Visual Studio 2008 and MSTest. We have a batch file that the
I'm using getopt to parse options and arguments. I wrote a batch file to
I'm trying to use a batch file to create another batch file... it's a
I wrote a batch file that uses the program curl.exe to download a database
I'm required to write a batch file to do a few things Initially I
I want to write batch file for my own php framework. for example i
I'd like to write a batch file that checks to see if a process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.