Solved

Batchfile: add to rightclick-menu in Windows explorer.

Posted on 2014-01-05
14
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 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 70

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 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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 70

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

Expert Comment

by:Gerwin Jansen, EE MVE
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 70

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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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