Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

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.

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

Open in new window


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

Open in new window


Can anybody find out where my code goes wrong?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I imagine Qlemo has it there, only thing I would say is that I would instead have a shortcut to the batch file on my desktop or similar and then drag the folder to process onto the shortcut.

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
Avatar of Stef Merlijn

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