# SingleInstanceAccumulator
See typical stack-o's expressing the need, e.g. this one.
The gist is it comes in handy as a bridge between selecting multiple files or folders in custom Windows File Explorer context menus and sending all the selections to various scripts or 3rd party tools.
"-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 character around each accumulated argument (default: ") - see examples below
-t:timeout millisecs (default: 200)
-w = hidden launch (hides console window for cmd.exe & powershell.exe)
-v = debug output
Heads up about registry entries:
HKEY_CURRENT_USER\Software\Classes\*\shell\xxxx\command
require full path on the main executable (e.g. c:\bin\SingleInstanceAccumulator.exe, not just SingleInstanceAccumulator.exe)... for some ungawdly reason of Windows, our path environment variable is not honored.HKEY_CURRENT_USER\Software\Classes\*\shell\
will map to all file types (via the asterisk "*"), which avoids needing to create an entry for each individual file type if you don't need it filetype specific.reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\MyCommand\command" /f /ve /t REG_EXPAND_SZ /d "\"^%%bin^%%\SingleInstanceAccumulator\" -w \"-c:myprogram.exe $files\" \"%%1\""
reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\Path2Clip\command" /f /ve /t REG_EXPAND_SZ /d "\"^%%bin^%%\SingleInstanceAccumulator\" -w -d:\" ^^^& echo \" \"-c:cmd /c (echo $files) ^^^| clip\" \"%%1\""
when run from windows explorer context menu the above reg entry will execute a command line that looks like this:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy bypass C:\bin\transcode.ps1 -listFilePath 'C:\Users\Beej\AppData\Local\Temp\tmp5EEF.tmp'
reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\MyCommand\command" /f /ve /t REG_EXPAND_SZ /d "\"^%%bin^%%\SingleInstanceAccumulator\" -w -f \"-c:powershell -ExecutionPolicy bypass \"^%%bin^%%\test.ps1 -listFilePath '$files'\" \"%%1\""
note: -listFilePath command line argument corresponds to test.ps1's $listFilePath param shown next below
corresponding test.ps1 sample:
param(
[String]$listFilePath
)
gc $listFilePath | % { $_ }
# erase $listFilePath # you probably want this in your final script as good cleanup, commenting out for debug
pause
side note if interested, i use this format to drive a transcode.ps1 script which leverages handbrake.exe command line to bulk process .mov files (from point and shoot cameras) into mp4's.
customizing windows explorer with context menus like this starts to turn it into an efficient workflow space... further on that note, i like to use a left-right double explorer tool
here is another example, setting up zip files with an auto-extract script: https://beej126.github.io/windows-file-explorer-auto-extract-zips-like-mac-finder/
reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\MyCommand\command" /f /ve /t REG_EXPAND_SZ /d "\"^%%bin^%%\SingleInstanceAccumulator\" -w -q:' \"-c:powershell -ExecutionPolicy bypass ^%%bin^%%\test.ps1 -filesArray $files\" \"%%1\""
will execute like this:
powershell test.ps1 -filesArray 'filepath1', 'filepath2', 'filepath3'
corresponding test.ps1 sample:
param(
[String[]]$filesArray
)
$filesArray | % { $_ }
pause
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer" /f /v MultipleInvokePromptMinimum /t REG_DWORD /d 100
-q:`'
) because otherwise your interactive powershell cli will get caught on the single tick as an open string... remember these SingleInstanceAccumulator command lines are primarily intended to be fired from shell > command registry entries and that initial command line execution context is not powershell, it is traditional cmd.exe syntax rules, so the -q:' is fine there:: get path where this script is started from
set cwd=%~dp0
::replace spaces with powershell escapes
set cwd=%cwd: =` %
:: crucial multi-file handling property - https://learn.microsoft.com/en-us/windows/win32/shell/context-menu-handlers?redirectedfrom=MSDN#employing-the-verb-selection-model
reg add "HKEY_CLASSES_ROOT\FileType\shell\YourNewContextMenu" /f /v "MultiSelectModel" /d "Player"
assoc .bth=ElevatorHiddenBatch
ftype ElevatorHiddenBatch=c:\bin\elevator.exe -hide -c \"%1\"
(fyi, Elevator.exe is another tool i've cobbled together to provide some more handy stuff in this space =)