1.

Solve : GOTO :END IF MORE THAN ONE INSTANCE FOUND RUNNING..???

Answer»

im COMPILING a batch file into a .EXE file with "quick batch file compiler"
but i need to have some code in the batch file that checks to see if this file is already running in a prior instance and if it is then it ends itself..

I tried the code below, but since it finds itself it just goes to the :END,
is there anyway to make it find 2 or more instances of itself and if so then make it go to :END
or is there anything else i could try..??

TASKLIST | FIND/I "BATCH.EXE" && GOTO END
START CALC
:END


try this :

@echo off
set thisfile=%~nx0
tasklist | find /i "%thisfile%" && exit
....
....
....


it will terminate itself if the process already runningthat does the exact same thing as my code does, it just finds itself and then ends itself..
is there anyway to to find more than 1 instances of the same proccess running and if so close all but ONE of them..??Don't know if this would WORK but give it a shot.

Quote

set /a n=0
set fn=%~nx0

for /f "usebackq delims=" %%I in (`tasklist | findstr /i /c:"%fn%"`) do (
set /a n+=1
)
if %n% equ 2 exit

Something like that would work but I don't have a bat2exe converter handy to test so the batch file just comes up as cmd.exe.
this is what HAPPEND...


Code: [Select]
C:\Documents and Settings\VM-WARE>"C:\Documents and Settings\VM-WARE\Desktop\batch.exe"

C:\Documents and Settings\VM-WARE>set /a n=0

C:\Documents and Settings\VM-WARE>set fn=batch.exe
| was unexpected at this time.
C:\Documents and Settings\VM-WARE>for /f "usebackq delims=" %I in (`tasklist | findstr /i /c:"batch.
exe"`) do (

C:\Documents and Settings\VM-WARE>
Quote
set /a n=0
set fn=%~nx0

for /f "usebackq delims=" %%I in (`tasklist ^| findstr /i /c:"%fn%"`) do (
set /a n+=1
)
if %n% equ 2 exit

Try that, I added a '^'.Thanx DeltaSlaya it seems to work ok now..

But i have a small problem though.
My app called BATCH.EXE opens in 4 instances of itself simultaneously at user logon which open 4 CMD windows, because i have BATCH.EXE set for startup in these 4 startup locations: { HK_LM , HK_CU , All Users StartUp Folder & Current Users StartUp Folder }
so your code works somewhat, but it will read out:
Code: [Select] if 4 equ 2 exitso i put in a few more lines like this
Code: [Select]if %n% equ 2 exit
if %n% equ 3 exit
if %n% equ 4 exit
so now all of the open cmd windows at user logon all close-exit because they all take the line:
Code: [Select]if %n% equ 4 exit

so now how is it possible to close all but one of them if all 4 are running simultaneously ..??
you have a design problem.Well I don't see why you need to start it from four places? Whats the point when you're going to close three anyway? The only way I see that you can do it, still won't be reliably is to have each one check at a different time, by pinging for a random amount of time...

eg

Quote
ping -n %random~0,1% localhost >nul

set /a n=0
set fn=%~nx0

for /f "usebackq delims=" %%I in (`tasklist ^| findstr /i /c:"%fn%"`) do (
set /a n+=1
)
if %n% gtr 1 exit

There would be other more complicated ways, and there's probably a simple way too. But I see this as an avoidable problem by not starting the program more than once in the first place...IS this code suppose to ping at a random number..?? because when i run it nothing seems to happen..
Code: [Select]ping -n %random~0,1% localhost >nul'>nul' suppresses the output so you won't experience anything apart from a hopefully random delay. You can make it further random by assigning a few random numbers to '-w %random:~0,2%'. For that to work you can't ping yourself, you have to ping a non-existent IP address.

OR, to do away with all the randoms you could just ping a website and the programs may each have marginally different response times.

Again, I'm sure this isn't the best way, there's probably another way thats 100% RELIABLE but I'd still like to know why you need to open it four times, it sounds, no offense, suspiciously like how a virus would like to open

EDIT: Woops, I see why it wasn't working, I missed out the semi-colon...

Quote
ping -n %random:~0,1% localhost >nul
Hey thanx for all your help DeltaSlaya..
But it just doesn't really seem to be working out for me though, because at least 2 instances of the process always seem to ping at the same random number and then they both close together.
so all instances of the process just wind up self terminating themselves..

the reason I use all 4 of those startup entires is just for fail-safe measures that I take to ensure that my File will always be ran at every users startup on my PC.

So my next question would be: What other programing languages {that are fairly easy to learn} could I transfer my Batch-App into that would allow me to code it, so it can only be opened in one instance of itself. Fail-safe? Such a procedure would be more appropriately named 'fail-prone'...

To add a startup registry entry simply create an entry in this key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

By saving the following as a *.reg file:

Quote
REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"keyname"="location of file"

Something like that should work I don't really know how to write .reg files.Hey, is there anyway to make my app install & run as a System Service...??
then check to see if the service is installed ok, and if it can't install itself as a system service then have it register itself to startup at: HK_LM & HK_CUWhat do you mean make your app install? It's only a batch file converted into executable format.

Again, I personally see no reason for making an application start at two different locations.

To make it a service it's just Services instead of Run in the key I believe, not 100% sure. But why would you want it to run before the operating system starts?

Yes you can check if the program is registered in the registry. 'REG QUERY /?' in a command prompt.


Discussion

No Comment Found