Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe multiple files into a single batch file (using explorer highlight)

I can already get a batch file to run when a user right clicks on a file type. How can I make it so that only one instance runs per highlighted group and gets all the files as arguments. Currently it runs single instance per file when a user "shift clicks"

there is most likely a better way to word this... you can see why I had trouble googling it.

thanks

like image 311
jtzero Avatar asked Sep 08 '25 12:09

jtzero


2 Answers

Normally a file association multi-selection invocation will start several instances of a program and the program itself would have to deal with it on its own (Or with the help of DDE or IDropTarget)

It is going to be very hard to implement this in a batch file, this example should get you started:

@echo off
setlocal ENABLEEXTENSIONS
set guid=e786496d-1b2e-4a49-87b7-eb325c8cc64d
set id=%RANDOM%
FOR /F "tokens=1,2,3 delims=.,:/\ " %%A IN ("%TIME%") DO SET id=%id%%%A%%B%%C
set sizeprev=0

>>"%temp%\%guid%.lock" echo %id%
>>"%temp%\%guid%.list" echo %~1

:waitmore
>nul ping -n 3 localhost
FOR %%A IN (%temp%\%guid%.list) DO set sizenow=%%~zA
if not "%sizeprev%"=="%sizenow%" (
    set sizeprev=%sizenow%
    goto waitmore
)
FOR /F %%A IN (%temp%\%guid%.lock) DO (
    if not "%%A"=="%id%" goto :EOF
    FOR /F "tokens=*" %%B IN (%temp%\%guid%.list) DO (
        echo.FILE=%%B
    )
    del "%temp%\%guid%.list"
    del "%temp%\%guid%.lock"
    pause
)

While this works, it is a horrible horrible hack and will fail badly if you don't wait for the first set of files to be parsed before starting a new operation on another set of files.

like image 166
Anders Avatar answered Sep 10 '25 07:09

Anders


If you create a batch file and place it on your desktop, then you can select multiple files and drop them on that batch file. They will get passed as multiple parameters to the file.

For example, assume you put dropped.bat on your desktop, and it looks like this:

@echo off
echo %*
pause

Now assuming you had three files x, y and z, if you multiple-selected them and dropped them on dropped.bat, you'd see a command window come up with this text in it:

C:\Users\alavinio\Desktop\x C:\Users\alavinio\Desktop\y C:\Users\alavinio\Desktop\z
Press any key to continue . . .

That's the closest you can get. The right-click-and-Open semantics expect to start a new executable for each selected item, and typically those executables check for another instance of themselves, and if they see one, send the parameter over there to that existing process, and terminate themselves. You can actually watch that happen with Task Manager or Process Explorer.

like image 23
lavinio Avatar answered Sep 10 '25 07:09

lavinio