[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batchfile: add to rightclick-menu in Windows explorer.

Posted on 2014-01-05
14
Medium Priority
?
612 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

0
Comment
Question by:Stef Merlijn
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39757318
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...)
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39757342
Change the script above from %~dp0 to %~dp1 (as told by dragon-it in the previous thread).
Run this command to create the folder context menu "MKV-Tool":
reg add HKCR\Directory\Shell\MKV-Tool\command /d "\"C:\PathToScript\Script.cmd\" \"%1\""

Open in new window

(Good reference for adding context menu entries via registry is at http://www.howtogeek.com/107965/how-to-add-any-application-shortcut-to-windows-explorers-context-menu/ ).
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39757381
@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?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:Stef Merlijn
ID: 39757403
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

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39757425
Is Gerwin's point valid? Thinking about it, %~dp1 should result in the parent folder, and instead we need to use %~f1 instead.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39757536
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
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39757655
Indeed, "%~1" is working just fine, tested with a simple batch file.
0
 

Author Comment

by:Stef Merlijn
ID: 39757912
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 :-)
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39758102
You're welcome, have a great 2014 as well!
0
 

Author Comment

by:Stef Merlijn
ID: 39758476
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?
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39758747
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).
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39758973
You can use (portable) Ant Renamer to remove/replace unwanted characters:

http://portableapps.com/apps/utilities/ant_renamer_portable
0
 

Author Comment

by:Stef Merlijn
ID: 39759046
Qlemo and Gerwin: Thank you both, I'll start with that.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

873 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