Solved

Batchfile: problem with variables

Posted on 2014-01-05
3
341 Views
Last Modified: 2014-01-05
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?
0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39757175
Is this your real code, or is it simplified for posting here? Because that is a rather clumsy way (because of the repetition of same stuff and hence much use of GOTO), and prone to typos and other errors, and more difficult to test.

I can't spot an error, so have to guess. You are not changing the folder to %~dp0, but use mixed relative paths and absolute paths.
BTW, you do not need to create the New folder in the loop - once is enough ;-).

My suggestion is:
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
setlocal EnableDelayedExpansion
CLS

REM *************************************************************************
REM Create new folder "New" inside current folder.
REM *************************************************************************
md "New" 2>nul

pushd %~dp0

for %%A in (*.mkv) do (

  REM *************************************************************************
  REM Only one filetype (extension) is processed
  REM *************************************************************************
  set ext=& for %%X in (idx sub srt) do IF EXIST %%~nA.%%X set ext=%%X
  if defined ext (
    REM *************************************************************************
    REM Merge subtitle *.srt/.sub/.idx into MKV-file with the same name
    REM *************************************************************************
    "C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "%%~dpANew\%%~nxA" "%%~fA" "--sub-charset" "0:ISO-8859-2" "--language" "0:dut" "--track-name" "0:Nederlands" "%%~dpnA.!ext!"
  ) else (
    REM *************************************************************************
    REM If no subtitle files were found, copy MKV file
    REM *************************************************************************
    if NOT EXIST "%%~dpANew\%%~nxA" COPY "%%~fA" "%%~dpANew\%%~nxA"
  )

  REM *************************************************************************
  REM Progress of merging files
  REM *************************************************************************
  Echo File processed: %%~nxA
)
Echo Merged MKV files are copied into subfolder %~dp0New\ 
pause
popd

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39757195
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
0
 

Author Comment

by:Delphiwizard
ID: 39757230
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:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28330859.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question