Home > OS >  For loop in cli batch
For loop in cli batch

Time:09-05

I've a bunch of videos that I need to batch remux with MKVtoolnix using cmd. For that I'm using this cmd. I'm not aware of how to run this cmd in loop.

This is the cmd:

"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output ^"E:\Output\Video - 001.mkv^" --language 0:mul --track-name ^"0:Video - 001^" --display-dimensions 0:768x576 --language 1:hi --track-name ^"1:Video - 001^" --language 2:ta --track-name ^"2:Video - 001^" --default-track-flag 2:yes --language 3:te --track-name ^"3:Video - 001^" ^"^(^" ^"E:\Batch\Video - 001.mkv^" ^"^)^" --attachment-name cover_small.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_small.jpg^" --attachment-name cover.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover.jpg^" --attachment-name cover_land.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_land.jpg^" --title ^"Video - 001^" --track-order 0:0,0:2,0:3,0:1

As of now I'm doing like this

"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output ^"E:\Output\Video - 001.mkv^" --language 0:mul --track-name ^"0:Video - 001^" --display-dimensions 0:768x576 --language 1:hi --track-name ^"1:Video - 001^" --language 2:ta --track-name ^"2:Video - 001^" --default-track-flag 2:yes --language 3:te --track-name ^"3:Video - 001^" ^"^(^" ^"E:\Batch\Video - 001.mkv^" ^"^)^" --attachment-name cover_small.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_small.jpg^" --attachment-name cover.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover.jpg^" --attachment-name cover_land.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_land.jpg^" --title ^"Video - 001^" --track-order 0:0,0:2,0:3,0:1

"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output ^"E:\Output\Video - 002.mkv^" --language 0:mul --track-name ^"0:Video - 002^" --display-dimensions 0:768x576 --language 1:hi --track-name ^"1:Video - 002^" --language 2:ta --track-name ^"2:Video - 002^" --default-track-flag 2:yes --language 3:te --track-name ^"3:Video - 002^" ^"^(^" ^"E:\Batch\Video - 002.mkv^" ^"^)^" --attachment-name cover_small.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_small.jpg^" --attachment-name cover.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover.jpg^" --attachment-name cover_land.jpg --attachment-mime-type image/jpeg --attach-file ^"E:\cover\cover_land.jpg^" --title ^"Video - 002^" --track-order 0:0,0:2,0:3,0:1

.....etc

I need to run this command in a loop for n times

CodePudding user response:

set /a serial=1000
:again
set /a serial =1
"C:\Program Files\MKVToolNix\mkvmerge.exe" ...
if %serial% leq 1234 goto again

Then replace each 001 in your mkvmerge.exe line with %serial:~-3% which will then execute mkvmerge.exe with the last 3 digits of the serial number.

The loop will execute 234 times with 1234 set as the limit, 001..234.


Here's the actual test file I created

@ECHO OFF
SETLOCAL
set /a serial=1000
:again
set /a serial =1
ECHO "C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output ^"E:\Output\Video - %serial:~-3%.mkv^"
if %serial% leq 1010 goto again
ECHO done...
GOTO :EOF

The first two lines simply suppress command-echoing and establish a local environment so that changes will be discarded when the batch terminates.

I truncated the mkvmerge line as the remainder should be more of the same, and echoed it since executing it makes no sense on my system.

Without the echo, the mkvmerge should run.

This is the output I obtained.

"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 001.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 002.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 003.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 004.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 005.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 006.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 007.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 008.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 009.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 010.mkv"
"C:\Program Files\MKVToolNix\mkvmerge.exe" --ui-language en --output "E:\Output\Video - 011.mkv"
done...
  • Related