拖放批处理文件为多个文件? [英] Drag and drop batch file for multiple files?

查看:175
本文介绍了拖放批处理文件为多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个批处理文件来使用PngCrush当我拖到优化的PNG图片拖放到该批处理文件。

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.

我的问题是:是否有可能创建一个批处理文件,就像我在后做了,但是能够在一次优化多张图片?拖放就可以了多个.png文件? (并具有输出是这样new.png,新的(1)。PNG,新的(2)。PNG等......

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...

推荐答案

是的,当然这是可能的。当拖动一个批处理文件,多个文件,你丢弃的文件作为一个空格分隔的列表清单。你可以用简单的以下批处理验证这一点:

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

现在你有两个选择:


  1. PngCrush已经能够处理给它的命令行上的多个文件名。的在这种情况下,所有你需要做的是通过%* 来PngCrush,而不是仅仅%1 (或许你现在要做的):

  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 %*

%* 包含所有参数的批处理文件,所以这是要通过的所有的参数到另一个程序的便捷途径。小心这样命名PngCrush选择,虽然文件。 UNIX爱好者都知道这个问题: - )

%* 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 :-)

读您的文章,描述你的技术后,但是,这将无法正常为你正在写的COM pressed文件 new.png 工作。一个糟糕的主意,如果你正在处理多个文件一次,因为只能有一个 new.png :-)。但我只是想指出,PngCrush处理多个文件刚刚好,所以如果你不介意的文件就地更新,然后把

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).

PngCrush不会处理多个文件的或的要每个图像写入后COM pression一个新的文件。的在这种情况下,你坚持你的一个文件,一次例行,但你遍历所有的输入参数。在这种情况下,最简单的方法,只是建立一个小的循环和每次处理一个时间参数:

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

我们在这里所做的很简单:首先,我们跳过整个批次如果不带参数运行,那么我们定义了一个标签跳转到:循环。内部,我们只需在第一个参数运行PngCrush,给COM pressed文件的新名称。你可能想在我这里使用的帮助呼叫道路清扫语法阅读起来。基本上我在这里做是完全一样的前命名该文件;我只是坚持_new到文件名的末尾(在扩展名之前)。 %〜dpn1 扩展到驱动器,路径和文件名(不含扩展名),而%〜X1 扩展到扩展,包括点。

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,我刚才读与new.png,新的(1).png等在这种情况下,你需要的输出,我们不需要任何花哨的路径解剖,但我们还有其他问题关心。

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.

最简单的方法很可能是在0至刚刚启动计数器,我们处理第一个文件之前,每次增加它我们处理另一个问题:

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%是我们这里的柜台,我们处理的情况下,其中 N 0通过编写结果 new.png 而不是新(0)巴纽

%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.

该方法有问题,虽然。如果已经有文件名为 new.png 新(x)的巴纽那么你可能会揍他们。不太好。因此,我们必须做不同的事情,并检查我们是否可以实际使用的文件名:

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.

随意根据需要适应。

这篇关于拖放批处理文件为多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆