There are a handful of issues with the way you're approaching the problem, as others have pointed out in comments - the biggest one, however, is that you're trying to use variables with different values on the same line; if %lng%
= eng
, it cannot also be spa
. Based on the code you provided, one of two solutions is likely your best bet:
There's no need for variables
If the language options are uniform for files and the %lngf%
value isn't predetermined (comes from a file, a property, etc.), but is something you're setting yourself - you can avoid your non-loop variables entirely:
mkvmerge -o "%%~nA.mkv" "%%A" --language 0:eng "%%~nA.en.srt" language 0:spa "%%~nA.es.srt"
You're not really saving any space or making it easier to digest or look at by changing things like eng
to %lng%
for no reason; I've also removed the ~
from your %%~A
since it was unnecessary.
OR
Your language variable is determined by what's available in a given file
If the value of %lngf%
is predetermined, you could use nested if exist else
lines to set each entire language argument of your command as a string in a variable, then use each one in your command. I haven't seen the data you're working with and am not familiar with mkvmerge
, but let's say that there are flags for each language available in your file:
FOR YOUR LOOP WITH PARAMETER %%A DO (
if exist englishflag (set "ENGLISH=--language 0:eng "%%%%~nA.en.srt" ") ELSE (set "ENGLISH=")
if exist spanishflag (set "SPANISH=--language 0:spa "%%%%~nA.es.srt" ") ELSE (set "SPANISH=")
etc. for each language
mkvmerge -o "%%~nA.mkv" "%%A" %ENGLISH%%SPANISH%%ETC%
)
This will use your language settings wrapped around your %%~nA
parameter for each iteration - note that since we're actually using percent signs in the variable we have to double them up. For available languages (those that have a flag or whatever has predetermined them), it sets that language's argument and a space as a variable - and for languages that are not available, their variable is empty. At the end of the loop it will use mkvmerge
and all of the language variables you have if exist else
statements for. Obviously this is not your for-loop, but structuring the contents of your loop this way should give you the desired output. If you have any questions on how to take this further just let us know.
1When comparing, the double equals (==) is correct. But when setting a variable, only one (=) is needed. And when setting the variable, you don't need the
%
characters.So the last part of that first line should be... set lng=eng
. – Doug Deden – 2019-01-16T17:50:24.8831It should be
IF %lngf%==en.srt SET lng=eng
. . . You need to be sure you set the variable value before theIF
statement evaluates it though. – Pimp Juice IT – 2019-01-16T20:10:22.990That didn't work. – G. L. – 2019-01-17T00:00:17.233
1Your For In Do is asking for variable
%lngf%
and you have not set it. – somebadhat – 2019-04-03T13:09:42.607IF %lngf%==en.srt SET lng=eng
.. you also want to quote around the variable in case it is empty.. you also want to quote around your set line so if there are trailing spaces you can't see, they don't get included in the variable. for instanceIF "%lngf%"=="en.srt" SET "lng=eng"
– Señor CMasMas – 2019-06-12T15:37:45.780