Home > other >  How can I reference part of a file name in a FOR loop?
How can I reference part of a file name in a FOR loop?

Time:09-17

Trying to mux video and audio using FFMPEG and a FOR loop. I have a folder full of files that look like this

movie1_audio.m4a
movie1_video.mp4
movie2_audio.m4a
movie2_video.mp4

I need a way to only reference everything before the underscore, rather than the whole file name. The below script looks for a matching .mp4 and .m4a file with identical names to match together. I'm trying to modify it to look for files that have identical names before the underscore.

FOR %%A IN (*.mp4) DO (
    ffmpeg -i "%%~nA.mp4" -i "%%~nA.m4a" -vcodec copy -acodec copy -movflags faststart "output\%%~nA.mp4"
)

I know I can batch rename my files to solve this easily, but if there's a way to only look at the first part of the filename in batch, I think that knowledge may be useful in future projects. Thanks!

CodePudding user response:

You just need another nested FOR command that utilizes a few of its options. I don't know how you need to fit into your FFMPEG command so I am leaving that up to you.

@echo off
FOR %%A IN (*.mp4) DO (
    FOR /F "tokens=1,* delims=_" %%G IN ("%%~A") DO (
        REM %%G will be the file name before the underscore.
        REM %%H will be everything after the underscore
        ffmpeg ......
    )
)
  • Related