Search code examples
loopsfor-loopcmdcopybinaryfiles

How to use command COPY to merge all MP3 files in a folder together to one MP3 file?


I try to merge *.mp3 files in a folder together into a single MP3 file by using command COPY and merge the files using option /b.

I have this command so far, but I am struggling to understand how to make it loop for each file in the folder.

for /f "tokens=*" %a in ('"dir /b *.mp3"') do echo copy /b %a+%a final.mp3

Mn my folder are:

D:\data\mp3_files\song one.mp3
D:\data\mp3_files\song two.mp3

I used to be able to do this before on multiple files compressing each file as you go along, but I don't know how to merge files using copy /b because the amount of files is unknown. So I don't know how many +%a I would use.

The syntax is: copy /b file1.mp3 + file2.mp3 targetfile.mp3

My issue is I don't know how to write file1.mp3 + file2.mp3 as variables.

I have this: copy /b %a+%a final.mp3

I don't know how many times to put the %a as I would not know how many files there are in the directory.

I also don't know why I would write the same variable twice.

Not sure how to code this properly.


Solution

  • That file merging task can be done with a batch file using following code:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "eol=| delims=" %%I in ('dir *.mp3 /A-D /B /ON 2^>nul') do copy /B "%%I" "TempFile.tmp" >nul & goto MergeFiles
    :MergeFiles
    for /F "skip=1 eol=| delims=" %%I in ('dir *.mp3 /A-D /B /ON 2^>nul') do copy /B "TempFile.tmp"+"%%I" "TempFile.tmp" >nul
    if exist "TempFile.tmp" ren "TempFile.tmp" "final.mp3"
    if exist "TempFile.tmp" del "TempFile.tmp"
    endlocal
    

    There is first just copied the first MP3 file in the current directory by command DIR ordered by file name to TempFile.tmp. It is important that the file extension of the temporary file is not .mp3 as otherwise the temporary file would be also in the list of MP3 files to process by the next FOR loop.

    The command GOTO results in exiting the first loop after making the copy of the first MP3 file.

    The second FOR loop runs like the first FOR loop in the background one more command process to execute DIR to get a list of file names with file extension .mp3 ordered by name which is captured by the command process processing the batch file and processed next line by line with ignoring empty lines not existing in output of DIR.

    For each file name the command COPY is executed to merge together the current temporary file with the current MP3 file from the list to create a new temporary file on which the current MP3 file is appended.

    Finally after appending one MP3 file after the other to the temporary file, the temporary file is renamed to the wanted name for the finally produced MP3 file or in an error case like final.mp3 is already existing in deletion of the temporary file.

    There could be used also the following batch file if the number of MP3 files is not too large and so string assigned finally to the environment variable FileList does not become longer than 8182 characters because of the environment variable name plus equal sign plus length of string assigned to the environment variable plus string terminating null cannot be more than 8192 characters.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "FileList="
    for /F "eol=| delims=" %%I in ('dir *.mp3 /A-D /B /ON 2^>nul') do call :AppendFileName "%%I"
    goto MergeFiles
    :AppendFileName
    set FileList=%FileList%+%1
    goto :EOF
    :MergeFiles
    if not defined FileList exit /B
    copy /B %FileList:~1% "final.mp3" >nul
    endlocal
    

    That batch files concatenates the file names of all MP3 files found in the current directory enclosed in " together to a single command line with + between each file name (and also at beginning of FileList string value) and runs finally the command COPY with all the file names (without the first plus sign) to merge them all at once together to file final.mp3.

    This second solution with the limitation of maximum string length is faster because of just one data copying is done on execution of the batch file.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • call /?
    • copy /?
    • del /?
    • dir /?
    • echo /?
    • endlocal /?
    • exit /?
    • for /?
    • if /?
    • goto /?
    • set /?
    • setlocal /?