Solved

Batchfile: problem with variables

Posted on 2014-01-05
3
337 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
3 Comments
 
LVL 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now