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?
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.
-----------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
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 :)
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?
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
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
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
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.
Yes, I'm using XP Media Center 2002 SP3, and it has been working for
every combination of file type and folder.