Batch file: How to play with loops and creating new directories


This Content is from Stack Overflow. Question asked by Ionut Bejinariu

I can’t find the solution.
I have the code below and I need your help to improve it.

Let’s say I have 5 songs in the directory musicdir and 100 lines with different .mp4 paths in mp4list.txt , 100 directories with the same 5 songs in each directory should be created. Canal-1, Canal-2Canal-100

When all songs in musicdir are exported in Canal-1 the loop will start again with the same songs from musicdir, with the difference that .mp4 videos will be exported in the next folder Canal-2 and so on.

but it is very important that the loop continues in the file mp4list.txtand continue where it left off in the previous directory in order not to repeat the same mp4 files

mp4list.txt contains the path’s of mp4 files, all are unique

even if from musicdir it will start from the beginning with exporting mp4 files in a new directory, in mp4list.txt it will continue where it left off in the previous directory

Batch will finish when all lines in mp4list.txt are finished.

I end up doing this code:

@echo off
setlocal enabledelayedexpansion
set "musicdir=MUZICA-wav-mp3"  
set "exportfoldername=Canal"
set "exportdir=Export%exportfoldername%
set "videodir=Video-15-sec"  

set n=0
md "%exportdir%"||call :a %n%

<"mp4list.txt" (
    for %%A in ("%musicdir%*") DO (
        set /p mp4list=
        ffmpeg -f concat -safe 0 -i "!mp4list!" -c copy mergedmp4.mp4
        ffmpeg -i mergedmp4.mp4 -i "%%A" -map 0:v -map 1:a -vcodec copy -c:a aac -b:a 320k "%exportdir%-%n%%%~nA.mp4"
        del mergedmp4.mp4"

set /a n+=1
md "%exportdir%-%n%"||goto a
exit /b

mp4list.txt is like this


and every output.txt have the path to mp4 file.
like this

file '1.mp4'
file '2.mp4'
file '3.mp4'
file '4.mp4'

thank you for your help


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?