0

I need to process some audio files. Each file must go through multiple sequential steps, for which I've written a batch file. But I want all the files in the folder to be processed in parallel, for speed. So I've written another batch file which calls the first, in a loop. But when I do that, I end up with multiple command windows, which stay open even when the batch file has closed.

In order of preference, how can I either (a) not have the windows open at all, or (b) have the windows minimised then close when finished; or (c) have the windows open then close when finished.

for %%I in (*.mp3) DO (
    start convert-mp3-to-m4b.bat "%%I"
)

I've managed to achieve (c), with start cmd /c convert-mp3-to-m4b.bat "%%I" /B, but that was more by luck than judgement.

Mark Barnes
  • 448
  • 3
  • 17
  • You are asking answers - use `CALL` key word : `call convert-mp3-to-m4b.bat "%%I"` – npocmaka Mar 02 '18 at 10:44
  • also check if the bat uses directly EXIT command and change it to `exit /b` – npocmaka Mar 02 '18 at 10:46
  • @npocmaka `call` won't run the processes in parallel... – Mark Barnes Mar 02 '18 at 10:50
  • then `start "" /b convert-mp3-to-m4b.bat "%%I"` – npocmaka Mar 02 '18 at 10:53
  • then `start "" /b convert-mp3-to-m4b.bat "%%I"` – npocmaka Mar 02 '18 at 10:54
  • 1
    `start /min` to run them minimized. Note: if you have too many `.mp3`, your computer may run out of resources. See [here](https://stackoverflow.com/a/44697124/2152082) to have max `` files running at a time. – Stephan Mar 02 '18 at 10:56
  • @LưuVĩnhPhúc `convert-mp3-to-m4b` doesn't sound like `play` for me. – Stephan Mar 02 '18 at 17:43
  • Audio processing is often CPU intensive. Running multiple jobs in parallel may not provide much benefit - in fact, too many jobs could actually slow things down. If you absolutely want to run in parallel, then surely there should be a max number of parallel processes. [This post](https://stackoverflow.com/a/11715437/1012053) shows one way to do this with pure batch. – dbenham Mar 02 '18 at 19:25
  • @dbenham - the answer I posted does exactly that. – Mark Barnes Mar 02 '18 at 19:29
  • So it does :-) Obviously I wrote my comment without reading the answer :-( – dbenham Mar 02 '18 at 19:46

1 Answers1

1

With the help of @Stephan I've ended up with this:

for %%Z in (*.mp3) DO (
    start "converting-audio" /min cmd /c convert-mp3-to-m4b.bat "%%Z"
)

This starts the windows minimised, and closes them after processing. I was also glad of the max <n> trick, although I had to adapt the code a bit. In case it's helpful to anyone else, my script now looks like this:

@echo off
setlocal enabledelayedexpansion
set count=0
set max=4
for %%Z in (*.mp3) DO (
    call :loop
    start "converting-audio" /min cmd /c convert-mp3-to-m4b.bat "%%Z"
)

goto :eof    

:loop
for /f "tokens=* USEBACKQ" %%G IN (`tasklist /fi "windowtitle eq converting-audio" ^| find "cmd.exe" /C`) do ( SET "count=%%G" )
if !count! GEQ !max! call :loop

This processes the files four at a time, and works brilliantly.

Mark Barnes
  • 448
  • 3
  • 17
  • Looks good. Instead of START /MIN I think you might be better off using START /B with stdout and stderr redirected to NUL. – dbenham Mar 02 '18 at 19:54