2

I'm learning batch script and I made some good progress (little, but enough to help me saving time). Now I'd like to work with variables. I've tried reading about variables and I know how to use %object% and replace it in some simple cases. But I don't have any idea on how to use them with "IF" statements. What I'm trying to do is replace element %lng% with "eng" when element %lngf% is "en.srt".

IF lngf==en.srt SET lng==eng
IF lngf==es.srt SET lng==spa
FOR /R %%A IN (*.avi *.mp4 *.webm *.ogm *.ogv *.flv) do mkvmerge -o "%%~nA.mkv" "%%~A" --language 0:%lng% "%%~nA.%lngf%" --language 0:%lng% "%%~nA.%lngf%"

**language appears 3 times because I usually have 3 languages for subtitles.

My output is:

mkvmerge -o "vsshort.mkv" "C:\Users\Administrator\Desktop\pessoal\tests\video.avi" --language 0:eng "vsshort." --language 0:eng "video."
mkvmerge v30.1.0 ('Forever And More') 64-bit
Erro: The file 'video.' file can't be opened for reading: open file error.

Note: it shows --language 0:eng twice. It should show --language 0:eng and then --language 0:spa.

  • 1
    When 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 Jan 16 '19 at 17:50
  • 1
    It should be IF %lngf%==en.srt SET lng=eng. . . You need to be sure you set the variable value before the IF statement evaluates it though. – Pimp Juice IT Jan 16 '19 at 20:10
  • That didn't work. – G. L. Jan 17 '19 at 0:00
  • 1
    Your For In Do is asking for variable %lngf% and you have not set it. – somebadhat Apr 3 '19 at 13:09
  • IF %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 instance IF "%lngf%"=="en.srt" SET "lng=eng" – Señor CMasMas Jun 12 '19 at 15:37
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.