Avatar of Stef Merlijn
Stef Merlijn
Flag for Netherlands asked on

Batchfile: add to rightclick-menu in Windows explorer.

Hi,


I would like to adjust the following batchfile.
I want to add it to a rightclick-menu of Windows explorer. When a user right-clicks on a folder, the batchfile will use that folder to merge all MKV-files with subtitle files.

What I need to know is:
- How to make the batch accept the selected folder
- How to add the batchfile to me rightclick-menu of Windows explorer.

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

Microsoft DOSWindows OS

Avatar of undefined
Last Comment
Stef Merlijn

8/22/2022 - Mon
Gerwin Jansen

Easiest would be to add a shortcut to the 'SendTo' location that points to your batch file:

C:\Users\<username>\AppData\Roaming\Microsoft\Windows\SendTo

I'd start by creating a shortcut to your batch file on the desktop and then copy that shortcut to the above location. This way you'll get an extra 'batch' item that answers your 2nd question.

(I'll have to check howto add the directory...)
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gerwin Jansen

@Qlemo - Thanks :)

I tested with %~dp1 - if I right-click c:\folder1\folder2 and echo %~dp1 in the batch file, I get c:\folder1 and not c:\folder1\folder2? Asker wants to right-click on ..\folder2 but it is taking the parent folder?
Stef Merlijn

ASKER
The "%1\" isn't added to the registry, but I've done that manually.

People how are interested can create file "MKVSubMerge.reg" with following content and run it "As Administrator".
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\MKVSubMerge\command]
@="\"C:\\Program files (x86)\\MKVSubMerge\\MKVSubMerge.cmd\" \"%1\\\""

Open in new window

This assumes that the batchfile is named: "MKVSubMerge.cmd"

Code for batchfile: "MKVSubMerge.cmd"
REM *************************************************************************
REM MKVSubMerge.cmd                                               version 0.2
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 0.2 Added functionality to add folder from contextmenu in Window Explorer
REM 
REM *************************************************************************
@echo off
setlocal EnableDelayedExpansion
CLS

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

pushd %~dp1

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" (
      Echo Making a copy of %%~fA
      Echo Please wait ...
      COPY "%%~fA" "%%~dpANew\%%~nxA"
    )
  )

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

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Qlemo

Is Gerwin's point valid? Thinking about it, %~dp1 should result in the parent folder, and instead we need to use %~f1 instead.
Steve Knight

We just need to use %~1 I suppose frankly, i.e.

if exist "%~1" (pushd "%~1"
) else (
  echo Folder not found
  pause
  exit /b
)

You can also have it prompt for a folder, e.g.

http://scripts.dragon-it.co.uk/links/batch-gui-folder

i.e. this will check for a folder dragged onto the shortcut, if not prompt for a folder using GUI.

@echo off

set folder=
if "%~1"=="" (
  echo No folder selected.
  call :getfolder
) ELSE (
  if exist "%~1" (
    set folder=%~1
  ) else (
    echo Folder not found or you have passed a filename instead
    pause
    exit /b
  )
)
if "%folder%"=="" exit /b
pushd "%folder%
echo Folder is %cd%

REM Put your stuff in here to work from the current directory, or use %folder% for the folder chosen...

pause

exit /b

:GetFolder
(echo Set objShell = CreateObject^( "Shell.Application" ^) 
 echo set objFolder = objShell.BrowseForFolder^( 0, "Select a folder", ^&H10^&, ""^)
 echo if objFolder is nothing then wscript.quit
 echo wscript.echo objFolder.Self.Path)> "%temp%\getpath.vbs"

 for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getpath.vbs"') do set folder=%%a

exit /b

Open in new window


Steve
Gerwin Jansen

Indeed, "%~1" is working just fine, tested with a simple batch file.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Stef Merlijn

ASKER
The solution I picked worked fine already, but to make sure I have changed the batchfile with the proposed setting by dragon-it (which also works fine).

Thank you all for the support. Have a great 2014 :-)
Gerwin Jansen

You're welcome, have a great 2014 as well!
Stef Merlijn

ASKER
One problem I discovered:
When a filename holds a "!" (exclamationmark) and probably other characters, then it isn't processed. Either with or without a subtitle-file present.
Any ideas? Maybe rename those files before processing?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Qlemo

Yes, any of !%:?^&/(), can make the script fail. You can catch one or another, but the safe methods are either to not have those characters at all (e.g. by renaming beforehand), or use a different command processor than cmd.exe (PowerShell, VBS come into mind).
Gerwin Jansen

You can use (portable) Ant Renamer to remove/replace unwanted characters:

http://portableapps.com/apps/utilities/ant_renamer_portable
Stef Merlijn

ASKER
Qlemo and Gerwin: Thank you both, I'll start with that.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.