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

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

问题描述

我编写了一个批处理文件,以便在将 .png 图像拖放到批处理文件中时使用 PngCrush 对其进行优化.

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、new(1).png、new(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 %*

%* 包含批处理文件的所有参数,因此这是将 all 参数传递给另一个程序的便捷方式.但是,请小心命名为 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 :-)

但是,在阅读了描述您的技术的帖子后,这将无法正常工作,因为您正在将压缩文件写入 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 不会处理多个文件 或者您想在压缩后将每个图像写入一个新文件.在这种情况下,您坚持使用一个文件"一次"例程,但您循环输入参数.在这种情况下,最简单的方法是构建一个小循环并在每次处理一个参数时shift:

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

我们在这里做的很简单:首先,如果它在没有参数的情况下运行,我们跳过整个批处理,然后我们定义一个标签以跳转到:loop.在里面,我们只是在第一个参数上运行 PngCrush,给压缩文件一个新名称.您可能想阅读我在 help call 中使用的路径解析语法.基本上我在这里所做的是完全像以前一样命名文件;我只是将_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.

预计到达时间: 哎呀,我只是用 new.png、new(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% 是我们这里的计数器,我们通过将结果写入 new.png 来处理 n 为 0 的情况,而不是new(0).png.

%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.pngnew(x).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.

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

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