Solved

Batchfile: problem with variables

Posted on 2014-01-05
3
336 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now