I'm new to VBS and in need of some code that will allow only once instance of the VBS File to be ran at a time..
so if the 1st instance is already running, a 2nd instance cannot be ran...
Is this possible at all or not..??
If so How do i code it..??
Like MrMilson has shown...a property you will be interested in looking is "CommandLine" which as previously stated is only available on WinXP or greater.
sorry its not working for me..!!
I'm getting this error:
this is my VBS code:
its suppose to start calc.exe and wait for a while so i can try an run a 2nd instance of the 1.vbs file, and if its already running it ends itself--- I hope...
whats wrong though- why am i getting that ERROR..??
------------------------------------------------
Set objWMIServie = GetObject("winmgmts:\\.\root\cimv2")
strQuery = "Select * from Win32_Process"
set ColItems = objWMIService.ExecQuery(strQuery,,48)
For Each objItem in colItems
msgbox objItem.name
Next
Set objApp = CreateObject("WScript.Shell")
objApp.run "CALC.EXE"
ok i just tried it in my VMWARE PC and i got no errors this time..
BUT.. all it does is just list all the currnetly running processes and then starts calc..
i made the wscript.sleep longer to 90000 so after calc was started I could try and run the script again.. But i was still able to run the 1.vbs again..
i thought this script was to prevent that from happening.. whats wrong..??
>i made the wscript.sleep longer to 90000 so after calc was started I could try and run the script again.. But i was still able to run the 1.vbs again..
What makes you think it will prevent anybody from running 1.vbs again and again and again at any time?
and the 1.vbs is just a simple code to start another EXE within the resource compartment of this compiled 1.vbs-exe and then it waits for like 1-min. so the 1.vbs-exe cant be ran in more than one instance of itself at the same time but for only like a 1-min.
thats why im asking for some code here, so i can prevent the 1.vbs-exe from running in more than one instance of itself..
>well thats what im asking. how can i make the 1.vbs to be able to run in only one instance of itself..??
well, it didn't sound like you were asking as a response to somebody's advice.
To response to your asking, add this script block at the top of your existing script. This script is based on the commandline property of win32_process instances. Hence, it is supported only for winxp up.
[tt]
'Functionality supported only for winXP up
dim svc, squery, ncount
set svc=getobject("winmgmts:root\cimv2")
squery="select commandline from win32_process " & _
"where (not ((commandline like ""%command.com%"") or (commandline like ""%cmd.exe%""))) and " & _
"(commandline like ""%[WC]Script.exe%" & wscript.scriptname & "%"")"
ncount=svc.execquery(squery).count
set svc=nothing
if ncount>1 then
wscript.quit
end if
'continue with the existing script
[/tt]
it doesn't work anymore..
is that because this code is only looking for wscript.exe and when my code is compiled into an exe an ran it doesn't use wscript.exe...??
How would I change that code so it will work when i compile it into an EXE with the ExeScript 3.0..???
I have no intention to try that commercial software (which said free to try without detail on the condition or expiry info).
In any case, if you have it "compile" to exe, say abc.exe, from the script named abc.vbs, I would suggest you try to change the query string from the above to this for testing.
[tt]
squery="select commandline from win32_process " & _
"where (not ((commandline like ""%command.com%"") or (commandline like ""%cmd.exe%""))) and " & _
"(commandline like [blue]""%" & replace(wscript.scriptname,".vbs",".exe") & "%""[/blue])"
[/tt]
(of couse you can fabricate case to break the simple use of replace like calling the vbs abc.exe.vbs of similar names, but that is just a guard, very artificial name. Just try to avoid those as a scripter has control on the name of the script name.)
From a broader point of view, I would suggest the vbs should not just be a wrapper spawning other process (vbs... or exe, com) to do some job. In that case, the application abc.exe would surround the control after spawning those processes and diminishing largely the usefulness of checking of the sort.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.