Stef Merlijn
asked on
Batchfile: problem with variables
Hi,
Below you find the content of a batchfile.
I have problems with the variables for the filenames.
Error: The system can't find the given file.
When I only execute this code it works fine:
Can anybody find out where my code goes wrong?
Below you find the content of a batchfile.
I have problems with the variables for the filenames.
Error: The system can't find the given file.
REM *************************************************************************
REM MergeSubWithMKV.exe version 0.1
REM -------------------------------------------------------------------------
REM
REM This batchfile will process all *.mkv files within the folder this
REM batchfile is placed in.
REM It will look for subtitle files with the same name as the MKV file.
REM f.e. movie.mkv -> movie.srt
REM
REM If a subtitle is found, then it will be merged with "MKVMerge.exe".
REM The merged MKV file will be placed in subfolder ..\New
REM
REM If no subtitles were found, the MKV file will be copied to folder ..\New
REM This way it is certain the all MKV-files are present in folder ..\New
REM
REM Version history:
REM 0.1 Initial version
REM
REM *************************************************************************
@echo off
CLS
for %%A in (*.mkv) do (
REM *************************************************************************
REM Create new folder "New" inside current folder.
REM *************************************************************************
md "New" 2>nul
REM *************************************************************************
REM Only one filetype (extension) is processed
REM *************************************************************************
IF EXIST %%~nA.srt GOTO FOUNDSUBSRT
IF EXIST %%~nA.sub GOTO FOUNDSUBSUB
IF EXIST %%~nA.idx GOTO FOUNDSUBIDX
REM *************************************************************************
REM If no subtitle files were found, process next MKV-file
REM *************************************************************************
GOTO ENDNOSUB
REM *************************************************************************
REM Merge subtitle *.srt into MKV-file with the same name
REM *************************************************************************
:FOUNDSUBSRT
"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "%~dp0New\%%~nxA" "%~dp0%%~nxA" "--sub-charset" "0:ISO-8859-2" "--language" "0:dut" "--track-name" "0:Nederlands" "%~dp0%%~nA.srt"
GOTO END
REM *************************************************************************
REM Merge subtitle *.sub into MKV-file with the same name
REM *************************************************************************
:FOUNDSUBSUB
"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "%~dp0New\%%~nxA" "%~dp0%%~nxA" "--sub-charset" "0:ISO-8859-2" "--language" "0:dut" "--track-name" "0:Nederlands" "%~dp0%%~nA.sub"
GOTO END
REM *************************************************************************
REM Merge subtitle *.idx into MKV-file with the same name
REM *************************************************************************
:FOUNDSUBIDX
"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "%~dp0New\%%~nxA" "%~dp0%%~nxA" "--sub-charset" "0:ISO-8859-2" "--language" "0:dut" "--track-name" "0:Nederlands" "%~dp0%%~nA.idx"
GOTO END
REM *************************************************************************
REM If no subs where found, copy MKV-file to new folder
REM *************************************************************************
:ENDNOSUB
if NOT EXIST "%~dp0New\%%~nxA" (
COPY "%~dp0%%~nxA" "%~dp0New\%%~nxA"
)
REM *************************************************************************
REM Progress of merging files
REM *************************************************************************
:END
Echo File processed: %%~nxA
)
Echo Merged MKV files are copied into subfolder ..\New\
pause
When I only execute this code it works fine:
for %%A in (*.mkv) do (
md "New" 2>nul
"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "%~dp0New\%%~nxA" "%~dp0%%~nxA" "--sub-charset" "0:ISO-8859-2" "--language" "0:dut" "--track-name" "0:Nederlands" "%~dp0%%~nA.srt"
Echo File processed: %%~nxA
if NOT EXIST "%~dp0New\%%~nxA" (
COPY "%~dp0%%~nxA" "%~dp0New\%%~nxA"
)
)
Echo Merged MKV files are copied into subfolder ..\New\
pause
Can anybody find out where my code goes wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Qlemo: your solution works perfectly.
Steve: I fully agree. I've opened a new question to make it a more workable solution.
I one of you want to have a go at it:
https://www.experts-exchange.com/questions/28330859/Batchfile-add-to-rightclick-menu-in-Windows-explorer.html
Steve: I fully agree. I've opened a new question to make it a more workable solution.
I one of you want to have a go at it:
https://www.experts-exchange.com/questions/28330859/Batchfile-add-to-rightclick-menu-in-Windows-explorer.html
Then instead of pushd using %0 the instead do the same but %1.
that is how I use various video / photo rename / cleanup batch files anyway.
Can't paste the line from phone as EE always chokes on the squiggly line character from my phone. You could also use in both ways by checking if %1 is blank and if not use batch file own dir.
steve