How to Pass MULTIPLE filenames to a Context Menu Shell Command?

15.7k views Asked by At

Passing a single filename to a context menu shell command is simple:

[HKEY_CLASSES_ROOT\*\shell\MyProgram\Command]
@="program.exe %1"

But if I select multiple files, program.exe is invoked for each such selected file.

What I would like to do instead is invokeprogram.exe only once, passing to it all the filenames currently selected.

How to do this?

4

There are 4 answers

0
Eternal Learner On BEST ANSWER

You may want to look at this post, as it says that this isn't really possible to pass multiple files to a single instance and you must rely on some form of IPC(Inter process Communication).

4
W4ldi On

You can use Send To for this. It supports multiple files.

In case this website goes offline:

Open shell:sendto with Windows + R or paste it into your explorer address bar. It should redirect you to:

C:\Users\<yourusername>\AppData\Roaming\Microsoft\Windows\SendTo

Create a shortcut to your program in this folder and you should see it in your explorer right-click menu under Send to

1
turgenev On

I created a small program that completely solves this problem. https://github.com/ge9/ExecuteCommand-Pipe

It is based on this Microsoft sample code: https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/Win7Samples/winui/shell/appshellintegration/ExecuteCommandVerb

It uses COM (Component Object Model) technology of Windows, so there are no restriction on the number or path length of passed files. Also it doesn't involve any inter-process communication, preserving the order of files. The command for opening files is specified in registry values. See the repository for details.

0
Martin On

Use: Command-Queuer.cmd

I wanted to do the same thing and ended up creating a 'wrapper' .cmd/bat file to queue the commands for me... I use a temporary queue file to: (a) self-nominate a control instance to run the process, and (b) signal to other instances not run the command(s) directly and instead just add their file/parameters to the queue and exit. The script waits X seconds for the other instances to queue their info and then processes the selected files sequentially.

Command-Queuer.cmd
------------------

@ECHO OFF
SETLOCAL

:: SETUP PARAMETERS: Control temp file location and delay before running
SET QueueFile="%TEMP%\Multi-Item-Queue.txt"
SET /A Secs=5

:: MAIN PROGRAM: If the first instance create the queue and wait, otherwise transfer to queue and exit
IF EXIST %QueueFile% ( ECHO %* >> %QueueFile% ) ELSE (
    ECHO %* > %QueueFile%
    ECHO Waiting %Secs% seconds for other files to finish queuing then will activate...
    TIMEOUT /T %Secs% /NOBREAK >nul
    
    REM - ADD YOUR CODE HERE TO PROCESS THE QUEUE FILE AS A WHOLE
    REM - Example: Display popup of all file paths selected: Msg %username% <%QueueFile%
    
    REM - ALTERNATIVELY, ITERATE THROUGH EACH LINE OF THE FILE
    REM - Example: FOR /F "tokens=*" %%Z in (%QueueFile%) DO ( COPY %%Z "C:\Backup" )
    
    :: Delete the queue file when finished
    DEL %QueueFile%
)
GOTO:EOF

NOTE: You will see a empty cmd window appear for a split-second for each file selected, i.e. if you select 30 files then 30 cmd windows will briefly appear, but these can be hidden if desired please check: Run a batch file in a completely hidden way (superuser.com)