-1

I'm trying to batch add cover art to all my MKV files in Windows using Batch scripting but I can't. My file structure is like this:

Video1.mkv
Video1.jpg

Both video and image have the same filename, the only difference is the extension.

I've tried the following command:

FOR %%f IN (*.mkv) DO (
mkvpropedit.exe "%%~nf.mkv" --attachment-name "cover" --attachment-mime-type 
"image/jpeg" --add-attachment "%%~nf.jpg"
)

It's weird how this command works for other apps and it's not working for MKVPropedit.

The result is:

Error: The file '%%~nf.jpg' could not be opened for reading: open file error.

My jpg files are not corrupt so I don't really know what that error means.

1

You should use a different variable letter in your FOR command. The letter f is one of the pathname format letters (the complete list is a, d, f, n, p, s, t, x). For example, use G as shown here:

FOR %%G IN (*.mkv) DO (mkvpropedit.exe "%%~nG.mkv" --attachment-name "cover" --attachment-mime-type "image/jpeg" --add-attachment "%%~nG.jpg")

According to SS64.com G is a good choice because it provides the longest run of letters that don't conflict with any of the pathname format letters:

%%G is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters. G > H > I > J > K > L > M

Further, if you're running this directly from the command prompt, you need to use single % characters, not double, as shown here:

FOR %G IN (*.mkv) DO (mkvpropedit.exe "%~nG.mkv" --attachment-name "cover" --attachment-mime-type "image/jpeg" --add-attachment "%~nG.jpg")

This Stack Overflow answer provides more information on using FOR in and outside of batch scripts.

  • Your command didn't work. I got the result: %%G unexpected at this time. – G. L. Jan 7 at 12:20
  • See updated answer. – Twisty Impersonator Jan 7 at 12:27
  • So there are differences? I didn't know. So if I want to use it in a .bat file, which is my intention, I should use it with %%G? If I want to use it directly from the command prompt, I should use %G? Very interesting. – G. L. Jan 7 at 12:30
  • See the link I provided for info about why you need to use two % only in a batch file. The coffee if the letter G is simply to avoid other letters that have other uses. – Twisty Impersonator Jan 7 at 12:32
  • Thanks a lot, man. Really nice. I was trying this for day and I didn't know about. I found about it right here: ss64.com/nt/syntax-args.html – G. L. Jan 7 at 12:34

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.