I have a problem with a .bat script called "ProcessMKV_EngAud_EngSub.bat" that utilizes MKVToolNix on Windows 10. This is the script:
@echo off
setlocal enabledelayedexpansion
REM ===========================
REM Configuration
REM ===========================
REM Destination directory where processed files will be saved
set "DEST_DIR=D:\Video\MKVConvert"
REM Path to the mkvmerge executable
set "MKVMERGE=C:\Program Files\MKVToolNix\mkvmerge.exe"
REM ===========================
REM Setup
REM ===========================
REM Create destination directory if it does not already exist
if not exist "%DEST_DIR%" (
mkdir "%DEST_DIR%"
)
REM ===========================
REM Processing
REM ===========================
REM Loop through each MKV file in the current directory
for %%a in (*.mkv) do (
REM Get the base name of the file without extension
set "FILE_NAME=%%~na"
code
REM Define the output filename
set "OUTPUT_FILE=%DEST_DIR%\%%~na.mkv"
REM Check if the output file already exists
if exist "!OUTPUT_FILE!" (
echo Warning: Output file "!OUTPUT_FILE!" already exists. Skipping...
) else (
REM Display a message indicating processing
echo Processing: %%~nxa
REM Execute mkvmerge to process the file
"%MKVMERGE%" ^
--ui-language en ^
--priority highest ^
--video-tracks 0 ^
--audio-tracks 1 ^
--subtitle-tracks 2 ^
--no-attachments ^
--no-global-tags ^
--no-track-tags ^
--no-chapters ^
--language 0:en ^
--track-name 0: ^
--default-track-flag 0:yes ^
--language 1:en ^
--track-name 1: ^
--default-track-flag 1:yes ^
--sub-charset 2:UTF-8 ^
--language 2:en ^
--track-name 2: ^
--default-track-flag 2:yes ^
--title "" ^
--track-order 0:0,0:1,0:2 ^
--output "!OUTPUT_FILE!" ^
"%%~fa"
REM Check if the processing was successful
if !errorlevel! equ 0 (
echo Successfully processed %%~nxa and saved as !OUTPUT_FILE!
) else (
echo Error: Failed to process %%~nxa. Please check the input file and try again.
)
)
Assistant
)
REM ===========================
REM Completion Message
REM ===========================
REM Display a completion message for processing
echo.
echo All MKV files have been processed and saved in %DEST_DIR%.
REM Pause the script to allow the user to see the output
pause
Here are the files it's supposed to run through:
The Cleveland Show (2009) - S02E02 - Cleveland Live!.mkv
The Cleveland Show (2009) - S02E09 - Beer Walk!.mkv
The Cleveland Show (2009) - S03E02 - The Hurricane!.mkv
The Cleveland Show (2009) - S03E16 - Frapp Attack!.mkv
The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins!.mkv
The Cleveland Show (2009) - S04E23 - Wheel! Of! Family!.mkv
Here is the output in the terminal once the script has been ran:
Processing: The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S02E09 - Beer Walk.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S02E09 - Beer Walk.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S03E02 - The Hurricane.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S03E02 - The Hurricane.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S04E23 - Wheel Family.mkv
mkvmerge v86.0 ('Winter') 64-bit
Error: The file 'D:\Video\The Cleveland Show (2009) - S04E23 - Wheel Family.mkv' could not be opened for reading: open file error.
Error: Failed to process The Cleveland Show (2009) - S04E23 - Wheel Family.mkv. Please check the input file and try again.
All MKV files have been processed and saved in D:\Video\MKVConvert.
Press any key to continue . . .
However, if I remove all the exclamation points ("!") from the filename, so that the files look like this:
The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv
The Cleveland Show (2009) - S02E09 - Beer Walk.mkv
The Cleveland Show (2009) - S03E02 - The Hurricane.mkv
The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv
The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv
The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv
Then the script has absolutely no issues at all and the output in the terminal once the script has been ran is this:
Processing: The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S02E02 - Cleveland Live.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S02E09 - Beer Walk.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S02E09 - Beer Walk.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S02E09 - Beer Walk.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S03E02 - The Hurricane.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S03E02 - The Hurricane.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S03E02 - The Hurricane.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S03E16 - Frapp Attack.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S04E12 - Pins, Spins and Fins.mkv. Please check the input file and try again.
Processing: The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv
mkvmerge v86.0 ('Winter') 64-bit
'D:\Video\The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv': Using the demultiplexer for the format 'Matroska'.
'D:\Video\The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv' track 0: Using the output module for the format 'HEVC/H.265'.
'D:\Video\The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv' track 1: Using the output module for the format 'AAC'.
Warning: 'D:\Video\The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv': A track with the ID 2 was requested but not found in the file. The corresponding option will be ignored.
The file 'D:\Video\MKVConvert\The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv' has been opened for writing.
The cue entries (the index) are being written...
Multiplexing took 2 seconds.
Error: Failed to process The Cleveland Show (2009) - S04E23 - Wheel Of Family.mkv. Please check the input file and try again.
All MKV files have been processed and saved in D:\Video\MKVConvert.
Press any key to continue . . .
Why is this happening and how can I fix it?
The issues you're encountering with your batch script stem from two primary factors:
Exclamation Marks (!) in Filenames:
Problem: In batch scripting, enabling delayed expansion (setlocal enabledelayedexpansion) allows variables to be expanded at execution time using !VAR! syntax. However, if your filenames contain exclamation marks (!), they interfere with this variable expansion mechanism, causing unexpected behavior or errors.
mkvmerge Exit Codes:
Problem: Even when filenames don't contain exclamation marks, mkvmerge may return a non-zero exit code (e.g., 1) due to warnings (like missing subtitle tracks). Your script currently treats any non-zero exit code as a failure, even if the processing was largely successful.
Solution Overview
To address these issues, you can modify your batch script in the following ways:
Handle Exclamation Marks in Filenames:
Approach: Temporarily disable delayed expansion within the loop when processing filenames to prevent exclamation marks from interfering with variable expansion.
Adjust Exit Code Handling:
Approach: Modify the script to treat certain non-zero exit codes (like 1, which indicates warnings but not critical failures) as successful operations.
Modified Batch Script
Here's the revised script incorporating the solutions:
batch
@echo off
REM Enable delayed expansion globally
setlocal EnableDelayedExpansion
REM ===========================
REM Configuration
REM ===========================
REM Destination directory where processed files will be saved
set "DEST_DIR=D:\Video\MKVConvert"
REM Path to the mkvmerge executable
set "MKVMERGE=C:\Program Files\MKVToolNix\mkvmerge.exe"
REM ===========================
REM Setup
REM ===========================
REM Create destination directory if it does not already exist
if not exist "%DEST_DIR%" (
mkdir "%DEST_DIR%"
)
REM ===========================
REM Processing
REM ===========================
REM Loop through each MKV file in the current directory
for %%a in (*.mkv) do (
REM Temporarily disable delayed expansion to handle '!' in filenames
setlocal DisableDelayedExpansion
REM Get the base name of the file without extension
set "FILE_NAME=%%~na"
REM Define the output filename
set "OUTPUT_FILE=%DEST_DIR%\%%~na.mkv"
REM Re-enable delayed expansion
setlocal EnableDelayedExpansion
REM Check if the output file already exists
if exist "!OUTPUT_FILE!" (
echo Warning: Output file "!OUTPUT_FILE!" already exists. Skipping...
) else (
REM Display a message indicating processing
echo Processing: %%~nxa
REM Execute mkvmerge to process the file
"%MKVMERGE%" ^
--ui-language en ^
--priority highest ^
--video-tracks 0 ^
--audio-tracks 1 ^
--subtitle-tracks 2 ^
--no-attachments ^
--no-global-tags ^
--no-track-tags ^
--no-chapters ^
--language 0:en ^
--track-name 0: ^
--default-track-flag 0:yes ^
--language 1:en ^
--track-name 1: ^
--default-track-flag 1:yes ^
--sub-charset 2:UTF-8 ^
--language 2:en ^
--track-name 2: ^
--default-track-flag 2:yes ^
--title "" ^
--track-order 0:0,0:1,0:2 ^
--output "!OUTPUT_FILE!" ^
"%%~fa"
REM Capture mkvmerge's exit code
set "MKG_MERGE_EXIT_CODE=!errorlevel!"
REM End the inner setlocal to revert to delayed expansion
endlocal
REM Check if the processing was successful or had only warnings
if "!MKG_MERGE_EXIT_CODE!"=="0" (
echo Successfully processed %%~nxa and saved as "!OUTPUT_FILE!"
) else if "!MKG_MERGE_EXIT_CODE!"=="1" (
echo Processed %%~nxa with warnings and saved as "!OUTPUT_FILE!"
) else (
echo Error: Failed to process %%~nxa. Please check the input file and try again.
)
)
REM End the temporary setlocal DisableDelayedExpansion
endlocal
)
REM ===========================
REM Completion Message
REM ===========================
REM Display a completion message for processing
echo.
echo All MKV files have been processed and saved in %DEST_DIR%.
REM Pause the script to allow the user to see the output
pause
Explanation of Changes
Handling Exclamation Marks:
Disable Delayed Expansion Temporarily:
Within the loop, before setting variables that might contain exclamation marks, setlocal DisableDelayedExpansion is called to prevent ! in filenames from being misinterpreted.
Re-enable Delayed Expansion:
After setting the necessary variables, setlocal EnableDelayedExpansion is invoked to continue using delayed expansion for other parts of the script.
Capturing mkvmerge Exit Code:
After running mkvmerge, the exit code is stored in a separate variable MKG_MERGE_EXIT_CODE before exiting the temporarily disabled delayed expansion environment. This ensures accurate capturing of the exit code without interference from ! characters.
Adjusting Exit Code Handling:
Interpret Exit Codes Appropriately:
The script now checks if mkvmerge returned 0 (success) or 1 (warnings). It treats both as successful operations, informing the user accordingly.
Only exit codes other than 0 or 1 are treated as fatal errors.
Alternative Approach: Using PowerShell
Batch scripts have inherent limitations when dealing with special characters like exclamation marks. If you continue to face challenges or require more robust handling, consider using PowerShell, which offers better support for complex string manipulations and special characters.
Here's a basic PowerShell script that mimics your batch script's functionality:
powershell
$destDir = "D:\Video\MKVConvert"
$mkvmerge = "C:\Program Files\MKVToolNix\mkvmerge.exe"
# Create destination directory if it doesn't exist
if (-not (Test-Path -Path $destDir)) {
New-Item -ItemType Directory -Path $destDir | Out-Null
}
# Get all MKV files in the current directory
Get-ChildItem -Path . -Filter "*.mkv" | ForEach-Object {
$inputFile = $_.FullName
$outputFile = Join-Path -Path $destDir -ChildPath $_.Name
if (Test-Path -Path $outputFile) {
Write-Host "Warning: Output file '$outputFile' already exists. Skipping..."
} else {
Write-Host "Processing: $($_.Name)"
$arguments = @(
"--ui-language", "en",
"--priority", "highest",
"--video-tracks", "0",
"--audio-tracks", "1",
"--subtitle-tracks", "2",
"--no-attachments",
"--no-global-tags",
"--no-track-tags",
"--no-chapters",
"--language", "0:en",
"--track-name", "0:",
"--default-track-flag", "0:yes",
"--language", "1:en",
"--track-name", "1:",
"--default-track-flag", "1:yes",
"--sub-charset", "2:UTF-8",
"--language", "2:en",
"--track-name", "2:",
"--default-track-flag", "2:yes",
"--title", "",
"--track-order", "0:0,0:1,0:2",
"--output", $outputFile,
$inputFile
)
# Start the mkvmerge process
$process = Start-Process -FilePath $mkvmerge -ArgumentList $arguments -NoNewWindow -PassThru -Wait
# Check the exit code
if ($process.ExitCode -eq 0) {
Write-Host "Successfully processed $($_.Name) and saved as $outputFile"
} elseif ($process.ExitCode -eq 1) {
Write-Host "Processed $($_.Name) with warnings and saved as $outputFile"
} else {
Write-Host "Error: Failed to process $($_.Name). Please check the input file and try again."
}
}
}
Write-Host "`nAll MKV files have been processed and saved in $destDir."
Pause
Advantages of Using PowerShell:
Better Handling of Special Characters: PowerShell natively handles strings with special characters more gracefully.
Enhanced Scripting Capabilities: Offers more advanced features and easier error handling.
Improved Readability: Scripts are generally easier to read and maintain.
Conclusion
By modifying your batch script to temporarily disable delayed expansion when handling filenames with exclamation marks and adjusting how exit codes from mkvmerge are interpreted, you can resolve the issues you're experiencing. However, for more complex scripting needs or better handling of special characters, transitioning to PowerShell is recommended.
If you need further assistance with the PowerShell script or additional modifications to the batch script, feel free to ask!