Batch processing multiple files at the same time [duplicate]

白昼怎懂夜的黑 提交于 2019-12-25 02:46:40

问题


I have this batch command

@echo off
FOR %%i in (C:\input\*.*) DO (
echo processing %%i
if not exist "C:\output\%%i" process.exe "%%i" -out "C:\output\%%i"
)
echo ---- finished ----
pause

Here my tool process.exe processes in a loop all files within a directory - if a result doesn't already exist.

Now my CPU is fast enough to run this process.exe on 2 or 3 files at the same time which would make the processing of the files much faster.

Question: How do I have to change the command to make my batch file processing 2-3 files at the same time?


回答1:


The following starts processes up to a max count of %bunch%. Whenever one of them finishes, another one will be started.

@ECHO off
setlocal enabledelayedexpansion
set bunch=3

for %%a in (C:\input\*) do (
  call :loop 
  echo processing: %%a
  start "MyCommand" cmd /c timeout 60
  REM if not exist "C:\output\%%i" start "MyCommand" cmd /c process.exe "%%i" -out "C:\output\%%i"

)
call :loop
goto :eof

:loop  REM waits for available slot
echo on
for /f %%x in ('tasklist /fi "windowtitle eq MyCommand"  ^| find /c "cmd.exe"') do set x=%%x
if %x% geq %bunch% goto :loop
echo off
goto :eof

I don't have your process.exe, so I have to guess. but the REMed line should work for you. (the timeout command is just to show the princip)



来源:https://stackoverflow.com/questions/49549269/batch-processing-multiple-files-at-the-same-time

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