Hi Stein Oiestad!
Meanwhile, your solution should be working.You asked your question quite a few days ago, but here I suggest to solve it with a "real" function "like %@isrunning[%0]"
My solution is not to ask for "%0", but for a footprint left by your own script. There are a few considerations beforehand:
The script should be recognized even if it is running with a different name, maybe started by a different process, and maybe in an older version.
So, we don`t rely on the filename "%0", but on a hard coded signature written to the running program`s TITLE.
Let us assume the script internally is named "SOV123: Stein O Version 1.2.3a".
One of the script`s lines looks like
TITLE SOV123: Stein O. Version 1.2.3a started %_date %_time %@crc[%_batchname]
Next thing is to find out whether there is another instance of SOV123 already running.
We can use a combination of TASKLIST and FFIND:
FUNCTION IsRUNNING=`%@eval[%@word["* ",0,^
%@execStr[tasklist | ^
ffind /UHKL /V /E"%@crc32[^
%@lfn[%_batchname]]"]]]`
FUNCTION IsRUNNING2=`%@eval[%@word["* ",0,^
%@execStr[tasklist | ^
ffind /UHKL /V /E"%fingerprint%"]]]`
( a hint: using /E"" instead of /T"" doesn`t harm...)
I have written a small demo around this function. It is tested with XP (mostly failed, donno why, maybe because of VM?) and 8.1 (OK),TCC Demo versions 12 and 17.
TCC/LE does _NOT_ work, of course.
Code:
[FONT=Courier New]@ECHO OFF
:: (tiny demo)
:: set a unique fingerprint
set fingerprint=SOV123
title %fingerprint %@crc32[%@lfn[%_batchname]]
:: this function identifies the EXACT copy of
:: the batch file. It does not need any parameter!
FUNCTION IsRUNNING=`%@eval[%@word["* ",0,^
%@execStr[tasklist | ^
ffind /UHKL /V /E"%@crc32[^
%@lfn[%_batchname]]"]]]`
::
:: some lines of code to test the function
::
:: just for DEMO purposes... set it to your needs, or
:: do it hard-coded as "1" in the IFF-clause below
set MAX=4
::
set Instances=%@IsRUNNING[%0]
::
title %0:instance=%instances% %@crc32[%@lfn[%_batchname]]
echo instance=%instances%; allowed: %max%
pause look at the TITLE! (press a key)
IFF %instances% gt %MAX% then
SETDOS /E\
msgBox OK "FORBIDDEN!" more than %max%\n instances NOT allowed!
exit
ENDIFF
echo running normally: %_batchname
echo Trying to start twice
start /PGM "%_batchname" & pause Any key to exit....
exit[/FONT]
[FONT=Arial]
:: Have a nice day!
[/FONT]
[FONT=Courier New][FONT=Arial]:: Tom[/FONT][/FONT]