批处理文件一个接一个 [英] Batch to process files one by one
问题描述
我有一个批处理文件来压缩PNG文件.当我批量拖放一些文件时,它开始同时处理所有文件.当我尝试数百个文件(确定)时,它变得毫无用处
I have a batch file to compress PNG files. When I drag and drop some files on batch, it starts to process all of them in the same time. And it becomes useless when I tried with hundreds of files (sure)
主要部分是这样的(简体)
START "" /W truepng.exe /out "%~n1_out1.png" %1
START "" /W pngwolf.exe --in="%~n1_out1.png" --out="%~n1_out2.png"
我认为我必须解决%1"部分. 我正在尝试批量处理具有4个文件块的文件.考虑两个不同的方法,都很好.
I think I must to solve the "%1" part. I'm trying to make batch to process files with 4-file blocks. Thinking of 2 different aproach, and both fine.
1..将继续处理其他4个文件
1. Will process the first 4 file, after continue with other 4
2.,否则它将同时运行最多4个"pngwolf.exe"实例(最佳选择)
2. Or it will run maximum 4 "pngwolf.exe" instances in the same time (best option)
在我的几次测试中,我开始尝试在%n之后使用计数器逻辑%1%2%3%4. 但是即使在那个阶段,我也坚持不懈,无法创建一个计数器系统. 无需询问,确保我是批处理编码方面的新手.但是,即使要成为一名优秀的设计师,看起来我也需要分批学习. 谢谢任何人的帮助.希望有一个我可以理解和适应的简单解决方案.
In my several tests, I started to try with a counter logic %1 %2 %3 %4 after %n... But even in that phase I stucked and couldn't create a counter system. No need to ask, sure I'm newbie on batch coding. But even to be a good designer, looks like I need to learn more on batch. Thanks for anyone will give a hand. Hope there is a simple solution that I can understand and adaptate.
万事如意
推荐答案
@ECHO OFF
SETLOCAL
SET /a instances=4
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
::
:loop
SET "nextfile=%~1"
IF NOT DEFINED nextfile (
ECHO all done
DEL "%tempfile%a*" >NUL 2>NUL
GOTO :eof
)
FOR /L %%a IN (1,1,%instances%) DO (
IF NOT EXIST "%tempfile%a%%a" (
>"%tempfile%a%%a" ECHO.
START "Instance %%a" oneconversion "%~1" "%tempfile%a%%a" %%a
SHIFT
GOTO loop
)
)
timeout /t 1 >NUL
GOTO loop
GOTO :EOF
oneconversion.bat 所在的位置
@ECHO OFF
SETLOCAL
START "" /W truepng.exe /out "%~n1_out1.png" %1
START "" /W pngwolf.exe --in="%~n1_out1.png" --out="%~n1_out2.png"
DEL "%~2" >NUL 2>NUL
cls
exit
为了证明这一原理,我将其用作 oneconversion.bat
As a dummy to prove the principle, I used as oneconversion.bat
@ECHO OFF
SETLOCAL
TITLE Processing %~1
SET /a wait=%~3*6
timeout /t %wait%
DEL "%~2" >NUL 2>NUL
CLS
exit
这就是为什么要提供第三个参数的原因-仅在其中可以使oneconversion.bat
变长.
which is why the third parameter is supplied - it's only there to make oneconversion.bat
variable-length.
操作非常简单:建立一个虚拟的临时文件,并假定文件名+数字可以用作标志文件.
The operation is fairly simple: establish a dummy temporary file and assume that that filename + a number is available as a flag file.
对于每个可用实例(设置为4-几乎可以是任何东西),检查标志文件,如果丢失,则创建它并发送下一个转换,并传递所需的文件名和temfilename.转换完成后,终止临时文件.
for each available instance (set to 4 - could be virtually anything) check for the flagfile, if it's missing, create it and despatch the next conversion, passing the required filename and temfilename. When the conversion's done, kill off the tempfile.
进行编辑以增强交流性:
Edit to be more communicative:
@ECHO OFF
SETLOCAL
SET /a instances=4
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
::
:loop
SET "nextfile=%~1"
IF NOT DEFINED nextfile (
IF EXIST "%tempfile%a" DEL "%tempfile%a"&ECHO All jobs started&GOTO loop
IF EXIST "%tempfile%a*" GOTO delay
ECHO all done
pause
GOTO :eof
)
FOR /L %%a IN (1,1,%instances%) DO (
IF NOT EXIST "%tempfile%a%%a" (
>"%tempfile%a%%a" ECHO.
START "Instance %%a" oneconversion "%~1" "%tempfile%a%%a" %%a
SHIFT
GOTO loop
)
)
:delay
timeout /t 1 >NUL
GOTO loop
GOTO :EOF
非常小的变化:
当发现nextfile
为not defined
时,则所有参数均已用尽.查找临时文件%tempfile%a"-该文件没有固定的实例号,并且已被删除.如果存在,则将其删除并显示消息所有作业已开始"-仍然有作业正在运行,但是将不再添加任何作业,然后返回循环.
When nextfile
is found to be not defined
, then all parameters have been exhausted. Look for the tempfile "%tempfile%a" - this has no affxed instance number and was being arbirarily deleted. If it exists, delete it and show the message "all jobs started" - there will still be jobs running, but no more will be addded, and go back to the loop.
下次找不到参数时,%tempfile%a"将不存在,因此请查看是否存在任何"%tempfile%a*"
-这将指示子批处理尚未完成.如果存在这样的文件,请转到delay
等待一秒钟,然后重试.
Next time no parameters are found, "%tempfile%a" will not exist, so see whether any "%tempfile%a*"
exists - which will indicate the subsidiary batch isn't finished. If such a file exists, go to delay
to wait a second and try again.
所有作业完成后,回显all done
并暂停以允许读取消息.
When all of the jobs have finished, echo all done
and pause to allow the message to be read.
自然地,如果您在显示的消息中插入Control-g字符,则如果您想发出声音警报,就会发出蜂鸣声.
Naturally, if you insert a control-g character into a displayed message, there will be a beep sounded if you want an audio alert.
这篇关于批处理文件一个接一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!