Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling a batch file from context menu

71 views
Skip to first unread message

ShadowTek

unread,
Jun 13, 2008, 7:10:04 AM6/13/08
to
I would like to create an entry in the context menu that will pass a
file of any type or a group of files of any type along to a batch
file. I don't really know that much about editing the registry, but I
am guessing that I have to create something in "HKEY_CLASSES_ROOT\*
\shellex\ContextMenuHandlers", and make it reference the batch file in
some way.

I read the thread at "http://groups.google.com/group/
alt.msdos.batch.nt/browse_thread/thread/
9a7fb7179f602368/8a4dd63d4e18fb35?hl=en&lnk=gst&q=context
+menu#8a4dd63d4e18fb35", so I am aware that groups of files invoked in
this manner will result in a separate calling of the batch file for
each file in a selected group of files, but is there any way around
this without using the "Send to" function? Someone commented that
"This approach is limited in how many files it can process at one time
because of a command line length limitation (1024 characters, I
think).", and I intend to use this new context menu item to be
functional with thousands of files at a time.

Also, I have seen references to the variable "%1" and "%L" when
attempting to pass values from the context menu to a batch file. What
is the difference between the two?

Harlan Grove

unread,
Jun 13, 2008, 9:40:02 PM6/13/08
to
ShadowTek <psistormyam...@cs.com> wrote...

>I would like to create an entry in the context menu that will pass a
>file of any type or a group of files of any type along to a batch
>file. I don't really know that much about editing the registry, but
>I am guessing that I have to create something in
>"HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers",
>and make it reference the batch file in some way.
...

You could kludge something.

The first call to the batch file would create a temp file by echoing
its filename argument into it. Subsequent calls would check if the
file already exists, and if so, they'd only append their filename
arguments into it.

The first call would record the temp file's size in an environment
variable, pause for a few seconds, then check if the temp file's size
had changed. If so, it'd repeat this record/pause/check process. If
not, it'd read through the files in the temp file. Once it's done
processing, it'd delete the temp file.

ShadowTek

unread,
Jun 15, 2008, 12:16:17 AM6/15/08
to
I still don't have any idea how to create a context menu item that
will pass the files along to the batch file. I cant' really do
anything else until I can figure out how to do that.

t.m.tr...@gmail.com

unread,
Jun 16, 2008, 4:47:48 AM6/16/08
to

-----------Add_context_menu_.reg
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\Shell\Send to echoargs.cmd\command]
@="C:\\Windows\\system32\\cmd.exe /k \"D:\\Temp\\scripts\\echoargs.cmd
\" \"%1\""
-----------EOF

Adding this to the registry will create a context menu entry 'Send to
echoargs.cmd'. Works for multiple selected files as long as they are
of the same type (invoking one cmd.exe per file). For something
working with heterogeneous collection of files I suppose you would
need to cook up your own context menu handler. Perhaps this link will
give you some ideas:
http://netez.com/2xExplorer/shellFAQ/bas_context.html

Cheers,

Tomek

ShadowTek

unread,
Jun 25, 2008, 9:39:14 PM6/25/08
to
I found that using the key "HKEY_CLASSES_ROOT\AllFilesystemObjects\"
will allow you to handle any combination of file types and folders at
the same time.

Your "kludge" works well. Thanks.

I managed to put it all together into simple, working system that will
pass along the names of all selected files to a batch file. The only
thing that that I didn't bother to build in to it was either a safety
check that limits the batch file to only 1 active instance, or a
method of using a different temp file for each simultaneous batch file
instance. But I am the only person that will be using this, so I won't
bother with it for now.

======= Start Contents of Registry Entry =======

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell]

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Custom Context Menu
Item]
@=""

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Custom Context Menu Item
\Command]
@="C:\\Windows\\system32\\cmd.exe /k call \"C:\\Program Files\\+ Batch
Files +\\01\\New.bat\" \"%1\""

======= End of Registry Entry ============


====== Start Contents of New.bat ===========

set Temptxt="C:\Program Files\+ Batch Files +\01\temp.txt"
if exist %Temptxt% (
echo %1 >> %Temptxt%
exit
) else (
echo %1 > %Temptxt%
ping 127.0.0.1 -n 2
call :CheckTempFileSize
call :ProcessFileList %Temptxt%
del %Temptxt%
set Temptxt=
)
goto :eof

:CheckTempFileSize
call :SetTempFileSizea %Temptxt%
ping 127.0.0.1 -n 2
call :SetTempFileSizeb %Temptxt%
if %TempFileSizea%==%TempFileSizeb% (
set TempFileSizea=
set TempFileSizeb=
goto :eof
) else (
call :CheckTempFileSize
goto :eof
)

:SetTempFileSizea
set TempFileSizea=%~z1
goto :eof

:SetTempFileSizeb
set TempFileSizeb=%~z1
goto :eof

:ProcessFileList
cd "%~dp1"
for /f "tokens=*" %%G in (%~nx1) do (
set WorkingFile=%%G
call :Actions01 %%G
)
goto :eof

:Actions01
// Do stuff with %1

====== End of New.bat ===========

Thanks for all the help :)

ShadowTek

unread,
Jun 28, 2008, 6:43:38 AM6/28/08
to
After more testing, I am experiencing problems now. I'm randomly
getting multiple instances of the first instance of the batch file,
the one that waits for the other instances to complete before
finishing its own code. I will repost the first part of the code.


set Temptxt="C:\Program Files\+ Batch Files +\01\temp.txt"
if exist %Temptxt% (
echo %1 >> %Temptxt%
exit
) else (
echo %1 > %Temptxt%
ping 127.0.0.1 -n 2
call :CheckTempFileSize
call :ProcessFileList %Temptxt%
del %Temptxt%
set Temptxt=
)
goto :eof


The only way that this can be happening is if the if the temp.txt does
not exist when a following instance of the batch file reaches the
first IF. The only reason that I could guess that this is happening is
that the batch files are being called in such rapid succession that
temp.txt can't be created before another batch file reaches that first
IF.

Any ideas on how to deal with this?

foxidrive

unread,
Jun 28, 2008, 8:19:58 AM6/28/08
to
On Sat, 28 Jun 2008 03:43:38 -0700 (PDT), ShadowTek <psistor...@cs.com>
wrote:

Why not code like this:


del %Temptxt% 2>nul
:loop
>>%Temptxt% echo/%1
shift
if not %1.==. goto loop

It's hard to tell what you are doing with that snippet


t.m.tr...@gmail.com

unread,
Jun 30, 2008, 6:55:03 AM6/30/08
to

Hi ShadowTek,

If you are right about this rapid invocation of batches than I don't
think that there is much you can do about it. The only thing that
comes to my mind is to do the list processing step from the last, not
first, invoked batch. Something like this (not tested):

set Temptxt="C:\Program Files\+ Batch Files +\01\temp.txt"

echo %1 >> %Temptxt%
for %%G in (%Temptxt%) do set TempFileSizeA=%%~zG
ping -n 1 -w 1000 1.1.1.1 > nul
for %%G in (%Temptxt%) do set TempFileSizeB=%%~zG
if not %TempFileSizeA%==%TempFileSizeB% exit /b
REM Temp file hasn't changed in 1 sec. so let's process it
call :ProcessFileList %Temptxt% & del %Temptxt%


This won't be very robust, though. It assumes that if temp file
doesn't change for 1 sec. then it's save to process it.


Cloud you explain, why it is necessary to aggregate all the files into
'temp.txt'. I don't see anything in your code that would require that
(e.g. counting or numbering those files). Couldn't you just process
them one-by-one as they are invoked with separate instances of
cmd.exe? Why go through this 'temp.txt' list at all?

Cheers,

Tomek

t.m.tr...@gmail.com

unread,
Jun 30, 2008, 10:40:07 AM6/30/08
to
On 26 Jun, 02:39, ShadowTek <psistormyam...@cs.com> wrote:
> I found that using the key "HKEY_CLASSES_ROOT\AllFilesystemObjects\"
> will allow you to handle any combination of file types and folders at
> the same time.

What system do you use? XP? I just tried that on Vista and it doesn't
work. Only homogeneous objects (folders or files of the same type) can
be handled that way.

Cheers,

Tomek

ShadowTek

unread,
Jun 30, 2008, 10:46:16 AM6/30/08
to
> Cloud you explain, why it is necessary to aggregate all the files into
> 'temp.txt'. I don't see anything in your code that would require that
> (e.g. counting or numbering those files). Couldn't you just process
> them one-by-one as they are invoked with separate instances of
> cmd.exe? Why go through this 'temp.txt' list at all?

One of the functions that I am using this for is to build a list of
files to process with an archiving utility. Having several hundred or
even several thousand simultaneous instances of archivers wouldn't be
good, especially with 7z ultra compression, lol.

I'll try out your suggestion when I have some more time. Thanks.

ShadowTek

unread,
Jun 30, 2008, 10:54:42 AM6/30/08
to
> What system do you use? XP? I just tried that on Vista and it doesn't
> work. Only homogeneous objects (folders or files of the same type) can
> be handled that way.

Yes, I'm using XP Media Center 2002 SP3, and it has been working for
every combination of file type and folder.

0 new messages