How to Have 2 variables using FOR work simultaneously in batch command?

599 views Asked by At

I am using mkvmerge to extract audio from multiple files. When doing that, I want to rename the extracted files so it is easier for me to merge them later.

My command right now is this

for /f "delims=" %%A IN ('dir /b *.mkv') DO mkvextract tracks "%%A" 2:F:\%%~nA.ogg

Which extracts an audio from a file say "x1.mkv" to "x1.ogg"

What I want to do is, rename the audio file (x1.ogg) to the name of another file from another folder. Kinda like this command (Which I know is wrong, I'm trying to tell what I want to achieve):

for /f "delims=" %%A IN ('dir /b *.mkv') && %%B in ('dir "F:\Show" /b *.mkv') DO mkvextract tracks "%%A" 2:F:\%%~nB.ogg

Which Takes a file from my current directory (%%A) , but the extracted audio will have the name of the corresponding file in another directory (%%B).

Thus,

x1.mkv => y1.ogg

x2.mkv => y2.ogg and so on..

So how do I type the FOR command in which 2 variables have names of files of 2 different directories?

Thanks!

1

There are 1 answers

4
Magoo On BEST ANSWER
@ECHO OFF
SETLOCAL
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=:" %%A IN (
 'dir /b /a-d "*.mkv"^|findstr /n /r "." '
 ) DO (
 FOR /f "tokens=1*delims=:" %%D IN (
  'dir /b /a-d "%destdir%\*.mkv"^|findstr /n /r "." '
  ) DO IF %%A==%%D echo(mkvextract tracks "%%B" 2:F:\%%~nE.ogg
)

GOTO :EOF

You would need to change the setting of destdir to suit your circumstances.

The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.

"filter" any line from the directory list containing any character, and number the line with a preceding #:. This has the effect of simply producing the same list, but numbered with #: preceding.

Tokenise each line into %%A and %%B using the separating : so the number goes to %%A and the filename to %%B.

With each %%A,%%B, do the same trick to the other directory, this time producing %%D,%%E, then if the line numbers match, execute your command with the appropriate selection of elements from the variables.