Core
Core

Reputation: 840

How do I prevent an infinite loop in windows batch file for in do command?

I have wav files I am normalizing with ffmpeg-normalize (a python program). My batch file is this for %%n in (*.wav) do ffmpeg-normalize "%%n" -nt peak -t 0 -o "%%n-norm.wav"

In my directory of 5 files, I get 5 -norm.wav files. Unfortunately then, the batch file creates 5 -norm.wav-norm.wav files and so on and so on. Why wouldn't it stop at the original list of 5 files?

Upvotes: 0

Views: 451

Answers (1)

aschipfl
aschipfl

Reputation: 34909

I think the problem is that a standard for loop does not fully enumerate the target directory in advance (see also this related thread), and that the output files also match the pattern (*.wav) for the input files. The first issue could be solved by using a for /F loop that parses the output of the dir command, so the complete file list is generated before looping even starts; the second issue could be solved by an additional filter constituted by findstr to exclude output files to become reprocessed (when the script is executed more often than once):

for /F "eol=| delims=" %%F in ('
    dir /B /A:-D-H-S "*.wav" ^| findstr /V /I "-norm\.wav$"
') do (
    ffmpeg-normalize "%%F" -nt peak -t 0 -o "%%~nF-norm.wav"
)

Upvotes: 1

Related Questions