We help IT Professionals succeed at work.

Batchfile: add to rightclick-menu in Windows explorer.

Stef Merlijn
Stef Merlijn asked
on
794 Views
Last Modified: 2014-01-06
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

Comment
Watch Question

Gerwin JansenTopic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2016

Commented:
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...)
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Gerwin JansenTopic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2016

Commented:
@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 MerlijnDeveloper

Author

Commented:
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

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

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

Commented:
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 JansenTopic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2016

Commented:
Indeed, "%~1" is working just fine, tested with a simple batch file.
Stef MerlijnDeveloper

Author

Commented:
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 JansenTopic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2016

Commented:
You're welcome, have a great 2014 as well!
Stef MerlijnDeveloper

Author

Commented:
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?
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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 JansenTopic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2016

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

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

Author

Commented:
Qlemo and Gerwin: Thank you both, I'll start with that.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.