Solved

Batchfile: add to rightclick-menu in Windows explorer.

Posted on 2014-01-05
14
535 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:Delphiwizard
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
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 68

Accepted Solution

by:
Qlemo earned 500 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 37

Expert Comment

by:Gerwin Jansen
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
 

Author Comment

by:Delphiwizard
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 68

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

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

Author Comment

by:Delphiwizard
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 37

Expert Comment

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

Author Comment

by:Delphiwizard
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 68

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 37

Expert Comment

by:Gerwin Jansen
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:Delphiwizard
ID: 39759046
Qlemo and Gerwin: Thank you both, I'll start with that.
0

Featured Post

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

746 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

12 Experts available now in Live!

Get 1:1 Help Now