How can I create a shell context menu item that takes multiple files as arguments?

11,637

Solution 1

You can also try adding the program to the SendTo menu.

Solution 2

Browsing for the answer it seems that there is no simple fix and that a shell extension is needed. Looking again at the registry entries for DiffMerge, it appears to use a shell extension: DiffMergeShellExtension64.dll. If P4Merge does not have such a shell extension then it looks like the only way I could get it working correctly in the Windows Explorer context menu would be to write one myself.

The Complete Idiot's Guide to Writing Shell Extensions series in Code Project is a useful guide to writing shell extensions. Part II of the series is about writing an extension that handles multiple files at once (exactly what I need).

Warning: The Complete Idiot's Guide to Writing Shell Extensions uses C and COM, ATL (Active Template Library) and MFC (Microsoft Foundation Classes). So writing a shell extension, if you're not familiar with those technologies, is going to be a long and potentially difficult process; it's definitely not something you can do in an hour.

Solution 3

You can do it with my program context-menu-launcher (singleinstance):

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"
Share:
11,637

Related videos on Youtube

SUMIT
Author by

SUMIT

I moved into IT after several years as an electronic engineer. Starting out at the bottom I worked my way up through VBA macros to Access then T-SQL (SQL Server) development. Since 2005 I've been doing middleware and backend development in C# and T-SQL. On the side I'm enjoying working with PowerShell, FitNesse and Azure DevOps. I'm also getting into Docker, Kubernetes and Azure. #SOreadytohelp

Updated on September 18, 2022

Comments

  • SUMIT
    SUMIT almost 2 years

    Creating a Windows Shell context menu item that takes a single file as an argument is easy. See this question for an example.

    However, how do you create a context menu item that takes multiple files as arguments? Say, for a diff-merge tool (in my case P4Merge), although the same technique would no doubt be applicable for other applications as well.

    I've installed P4Merge but it does not add an item to the context menu automatically so I will have to do it manually.

    When I tried using:

    "C:\Program Files\Perforce\p4merge.exe" %1 %2
    

    as the command line I got an error:

    Errors: At least two files are needed. Cannot open only one file. P4Merge needs 0, 2, or 3 files.

    When I tried using:

    "C:\Program Files\Perforce\p4merge.exe" %0 %1
    

    as the command line it opened two instances of P4Merge, one for each file.

    It appears the correct file names are being passed through to %0 and %1 but a different instance of the P4Merge application is being executed for each one.

    I currently have SourceGear's DiffMerge tool which has an item on the Shell context menu and that works beautifully, as I am able to select two files and use the context menu item to run a diff on them. I've trying searching the registry to see what arguments DiffMerge uses but I could not find a DiffMerge commandline that included arguments.

    • Synetech
      Synetech almost 12 years
      As you found out, there is no way to do what you want with a simple registry hack. There is only %1 because the extension only applies to one object and is executed separately for multiple objects. It requires programming a full-on shell-extension. There are however some programs/shell-extensions that let you do some more advanced context-menu stuff than a registry hack will allow. (The only one I can remember is MMM.) Unfortunately, I think they tend to be limited to letting you make submenus rather than allow for multiple objects to be dropped.
  • Synetech
    Synetech almost 12 years
    Yup, that works nicely.
  • SUMIT
    SUMIT over 11 years
    Worked perfectly. Here are instructions for how to add an application to the SendTo menu in Windows 7. There was no need to add any command-line parameters, just dropped the application shortcut into the %APPDATA%\Microsoft\Windows\SendTo folder. Back in Windows Explorer, selecting two files then right-clicking and selecting Send To > P4Merge performed a diff on the selected files.
  • SUMIT
    SUMIT almost 9 years
    Works like a charm, thanks. Is there any way to associate the singleinstance application with any file type, without adding it to the file associations for each file type individually? For example, I currently use DiffMerge for comparing things like XML files, SQL scripts, config files, *.cs files, .gitignore files, plus others.