How to add new items to right-click event on Folders and Files in Windows?
Asked Answered
F

4

9

I did google couple of tutorials on google.

I am able to add right-click menu item to a FOLDER by doing this:

[HKEY_CLASSES_ROOT\Directory\shell\Command]
@="TestRightClick:"

[HKEY_CLASSES_ROOT\Directory\shell\Command\Command]
@="myExecutable.exe %L"

I need to add this to a FILE as well.

1) Where do I add it in the registry?

2) And how do I pass parameters to my executable in case if I am selecting multiple files?

Related:

How to pass in multiple file/folder paths via a rigth-click event(verb) to an executable?

Famous answered 30/11, 2009 at 18:50 Comment(0)
C
4
  1. Files have context menus by extension. Add your Command registry keys to the appropriate extension or HKEY_CLASSES_ROOT\* to affect all files.
  2. You can use %1 to pass the filename to the application (much like you've indicated with %L above). If you select multiple files, each will be called separately, as if you right-clicked each one individually.

I'm not aware of any easy way to pass multiple items from a right-click context menu to one executable instance.

Carley answered 30/11, 2009 at 22:26 Comment(2)
@2. any suggestion on how to avoid calling my application multiple times and still pass in multiple file paths. Maybe with threading?Famous
Some more details for this solution: I added a key to HKEY_CLASSES_ROOT*\Shell\TestRightClick then I added Command key HKEY_CLASSES_ROOT*\Shell\TestRightClick\Command after that passed in my executable to a Default stringFamous
Z
12

You can do it with my program singleinstance. No shell extensions involved.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.txt\Shell\p4merge]
"MultiSelectModel"="Player"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.txt\Shell\p4merge\Command]
@="\"d:\\singleinstance.exe\" %1 \"C:\\Program Files\\Perforce\\p4merge.exe\" $files --si-timeout 400"
Zygote answered 26/7, 2015 at 12:15 Comment(1)
i tried your solutions.. i downloaded singleinstance.exe after create command with your defined value. but i got Windows cannot access the specified device, path, or file. You may not have the appropriate permission to access the item. this error.Hiller
C
8

The key word you're looking for is 'verbs' or 'handlers' not 'events'.

Context menu verbs for particular file extensions can be placed under the ProgID for the file type, the Perceived Type key (if the file type has a perceived type), the AllFileSystemObjects key, or the Base Class Key (*).

Note that writing to these keys in the HKEY_CLASSES_ROOT hive will redirect the writes to HKEY_LOCAL_MACHINE\Software\Classes, and will require elevated privileges. If you write to the HKEY_CURRENT_USER\Software\Classes tree, you can do this with standard user rights.

It's up to you to handle a scenario where multiple files are selected. One instance of your application will be launched per file you have selected. You can solve this by checking if another instance of your application is running, and using Inter-Process Communication to notify the existing instance that other extensions have been selected.

On MSDN, be sure to read

Covering answered 1/12, 2009 at 4:48 Comment(0)
C
4
  1. Files have context menus by extension. Add your Command registry keys to the appropriate extension or HKEY_CLASSES_ROOT\* to affect all files.
  2. You can use %1 to pass the filename to the application (much like you've indicated with %L above). If you select multiple files, each will be called separately, as if you right-clicked each one individually.

I'm not aware of any easy way to pass multiple items from a right-click context menu to one executable instance.

Carley answered 30/11, 2009 at 22:26 Comment(2)
@2. any suggestion on how to avoid calling my application multiple times and still pass in multiple file paths. Maybe with threading?Famous
Some more details for this solution: I added a key to HKEY_CLASSES_ROOT*\Shell\TestRightClick then I added Command key HKEY_CLASSES_ROOT*\Shell\TestRightClick\Command after that passed in my executable to a Default stringFamous
T
0

See GitHub SingleInstanceAccumulator for a C# implementation of the well worn Mutex + COPYDATA approach to this.

other stack-o's expressing the need.

Explorer Context Menu config

::creates the entry
:: and crucial multi-file handling property
reg add "HKEY_CLASSES_ROOT\FileType\shell\YourNewContextMenu" /f /v "MultiSelectModel" /d "Player"

::your desired command line
reg add "HKEY_CLASSES_ROOT\FileType\shell\YourNewContextMenu\command" /f /ve /t REG_EXPAND_SZ /d "***see command line examples***"

e.g. On my system, for ".mov" files, I would replace FileType above with VLC.mov

Complex REG ADD example

Replace "* see command line examples *" above with your desired command line.
Note: quotes & environment variables must be escaped and escaping work slightly differently for the initial command versus later in the string!?!

λ reg add "HKEY_CLASSES_ROOT\VLC.mov\shell\Transcode\command" /f /ve /t REG_EXPAND_SZ /d "\"^%bin^%\SingleInstanceAccumulator\" -f \"-c:powershell -ExecutionPolicy bypass "\"^%bin^%\test.ps1\"" -list '$files'\" \"%1\""

SingleInstanceAccumulator.exe Usage

"-c:command line" (default: cmd /c echo $files && pause)
  $files will be replace with aggregated list

-f = output each item on separate line to new tempfile
  $files will be replaced by the tempfile path
  quote will default to nothing

-d:delimiter (default: ,)
-q:quote (default: ")
-t:timeout millisecs (default: 200)
-w = hidden launch
-v = debug output

Command Line Examples

note: initial command must have path for shell > command to work

PowerShell & temp file

note: -f usage

"%bin%\SingleInstanceAccumulator" -f "-c:powershell -ExecutionPolicy bypass "%bin%\test.ps1" -list '$files'" "%1"

PowerShell & inline files list

note: -q usage

"%bin%\SingleInstanceAccumulator" -q:' "-c:powershell -ExecutionPolicy bypass "%bin%\test.ps1" -list $files" "%1"

test.ps1 (with temp file)

powershell
param(
  [String]$listFilePath
)

gc $listFilePath | % { $_ }

pause

erase $listFilePath

pause

test.ps1 (with files array parm)

param(
  [String[]]$filesList
)

$filesList | % { $_ }

pause
Teapot answered 18/2, 2018 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.