Drag and drop batch file for multiple files?

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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:

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.



回答2:

To do Drag & Drop in a secure way, isn't so simple with batch.

Dealing with %1, shift or %* could fail, because the explorer is not very smart, while quoting the filenames, only filenames with spaces are quoted.
But files like Cool&stuff.png are not quoted by the explorer so you get a cmdline like

pngCr.bat Cool&stuff.png 

So in %1 is only Cool even in %* is only Cool, but after the batch ends, cmd.exe tries to execute a stuff.png (and will fail).

To handle this you could access the parameters with !cmdcmdline! instead of %1 .. %n, and to bypass a potential error at the end of execution, a simple exit could help.

@echo off setlocal ENABLEDELAYEDEXPANSION rem Take the cmd-line, remove all until the first parameter set "params=!cmdcmdline:~0,-1!" set "params=!params:*" =!" set count=0  rem Split the parameters on spaces but respect the quotes for %%G IN (!params!) do (   set /a count+=1   set "item_!count!=%%~G"   rem echo !count! %%~G )  rem list the parameters for /L %%n in (1,1,!count!) DO (   echo %%n #!item_%%n!# ) pause  REM ** The exit is important, so the cmd.ex doesn't try to execute commands after ampersands exit 

Btw. there is a line limit for drag&drop operations of ~2048 characters, in spite of the "standard" batch line limit of ~8192 characters.
As for each file the complete path is passed, this limit can be reached with few files.



回答3:

FOR %%A IN (%*) DO (     REM Now your batch file handles %%A instead of %1     REM No need to use SHIFT anymore.     ECHO %%A ) 

And to differentiate between dropped files and folders, you can use this:

FOR %%I IN (%*) DO (     ECHO.%%~aI | FIND "d" >NUL     IF ERRORLEVEL 1 (         REM Processing Dropped Files         CALL :_jobF "%%~fI"     ) ELSE (         REM Processing Dropped Folders         CALL :_jobD "%%~fI"     ) ) 


回答4:

You don't need a batch script to optimize multiple PNGs, all you need is the wildcard:

pngcrush -d "crushed" *.png 

That will pngcrush all PNGs in the current dir and move them to a sub-dir named "crushed". I would add the -brute flag to likely shave off a few more bytes.

pngcrush -d "crushed" -brute *.png 

I'm posting this because it doesn't seem to be well documented or widely known, and because it may be easier for you in the long run than writing and maintaining a drag and drop batch file.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!