batch-file

How to combine batch files


I have 4 batch files I run for MKV files. The first renames all the files, the second removes any comments in all the files, the 3rd edits the "Title" in the metadata to match the filenames and the 4th creates a directory of each filename, then moves the mkvs into them.

All 4 files work perfectly but I'm trying to combine them into a single batch file. I've managed to do it, and it's not pretty, but it works. The problem is, when all the files are done, gone through all of the processes, the batch file won't end. It keeps restarting over and over.

Here is the combined batchfile...

@ECHO OFF
@echo Renaming Files...
pause
SETLOCAL EnableExtensions DisableDelayedExpansion
set "mkv=C:\Users\Immortalis\Downloads\-=UT=-\! SAVED DOWNLOADS\mkvtoolnix\mkvpropedit.exe"
pushd "."
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n') DO (
   SET "_FFN=%%~fG"                                    File Full Name
   SET "Var=%%~nxG"                                    File Name + Extension
   call :renaming
)
popd
ENDLOCAL
goto :EOF

:renaming
   SET "Var=%Var:(1={1%"
   SET "Var=%Var:(2={2%"
   SET "Var=%Var:0)=0}%"
   SET "Var=%Var:1)=1}%"
   SET "Var=%Var:2)=2}%"
   SET "Var=%Var:3)=3}%"
   SET "Var=%Var:4)=4}%"
   SET "Var=%Var:5)=5}%"
   SET "Var=%Var:6)=6}%"
   SET "Var=%Var:7)=7}%"
   SET "Var=%Var:8)=8}%"
   SET "Var=%Var:9)=9}%"
   SET "Var=%Var:(Ur)=(U)%"
   SET "Var=%Var:(Unrated)=(U)%"
   SET "Var=%Var:.trim01=%"
   SET "Var=%Var:[DR]=[D]%"
   SET "Var=%Var:[WR]=[W]%"
   SET "Var=%Var:[HD]=[H]%"
   SET "Var=%Var:[BR]=[B]%"
   SET "Var=%Var:[s={s%"
   SET "Var=%Var:00]=00}%"
   SET "Var=%Var:01]=01}%"
   SET "Var=%Var:02]=02}%"
   SET "Var=%Var:03]=03}%"
   SET "Var=%Var:04]=04}%"
   SET "Var=%Var:05]=05}%"
   SET "Var=%Var:06]=06}%"
   SET "Var=%Var:07]=07}%"
   SET "Var=%Var:08]=08}%"
   SET "Var=%Var:09]=09}%"
   SET "Var=%Var:10]=10}%"
   SET "Var=%Var:11]=11}%"
   SET "Var=%Var:12]=12}%"
   SET "Var=%Var:13]=13}%"
   SET "Var=%Var:14]=14}%"
   SET "Var=%Var:15]=15}%"
   SET "Var=%Var:16]=16}%"
   SET "Var=%Var:17]=17}%"
   SET "Var=%Var:18]=18}%"
   SET "Var=%Var:19]=19}%"
   SET "Var=%Var:20]=20}%"
   SET "Var=%Var:21]=21}%"
   SET "Var=%Var:22]=22}%"
   SET "Var=%Var:23]=23}%"
   SET "Var=%Var:24]=24}%"
   SET "Var=%Var:25]=25}%"
   SET "Var=%Var:26]=26}%"
rename "%_FFN%" "%Var%"
@echo Removing Comments...
pause
for /R "." %%I in (*.mkv) do "%mkv%" "%%I" --tags all: -d title --delete-attachment 1
@echo Setting Title to filename...
pause
for /R "." %%I in (*.mkv) do "%mkv%" "%%I" --edit info --set title="%%~nI"
@echo Placing files in separate directories...
pause
 if not "%%~ni" == "organize" (
  md "%%~ni" && move "%%~i" "%%~ni"
 )
)
goto :EOF

:EOF
@echo Processes Complete!
pause

Solution

  • If you want to combine your batch files into one you can use this structure (let's assume you have PartOne.cmd, PartTwo.cmd, PartThree.cmd, PartFour.cmd as separate files):

    File AllParts.cmd:

    @echo off
    SETLOCAL EnableExtensions DisableDelayedExpansion
    echo Start processing...
    
    REM Here is a good place for parameters used by all parts - Store them
    REM in environment variables to be used across the parts
    
    call :PartOne
    call :PartTwo
    call :PartThree
    call :PartFour
    goto :Done
    
    :PartOne
    REM ... your code of PartOne.cmd ...
    exit /b
    
    :PartTwo
    REM ... your code of PartTwo.cmd  ...
    exit /b
    
    :PartThree
    REM ... your code of PartThree.cmd  ...
    exit /b
    
    :PartFour
    REM ... your code of PartFour.cmd  ...
    exit /b
    
    :Error
    ECHO Error occurred: %errormsg%. Exiting with code 1 ...
    ENDLOCAL
    EXIT 1
    
    :Done
    ENDLOCAL
    ECHO Processing done!
    EXIT 0
    

    Copy the parts of your cmd files into this single file and test it. Essentially this converts each file into a subroutine which resides now in the new file AllParts.cmd. Each subroutine is then called in sequential order (e.g. call :PartOne).

    Note: