Sign in to follow this  
Followers 0
NDog

Detect multiple instances of script

3 posts in this topic

Hi

Unfortunately I'm in a situation where I am forced to use .vbs script files to do a task. I need to be able to detect if a particular script has been run multiple instances, however it may not the exact same file, eg patha\script.vbs and pathb\script.vbs. The idea is that only one can run at a time and if a second script is launched it should exit. However that is not to say that any vbscript type process should exit, only these scripts that are 'marked' as such.

In autoit you can do simple 3 lines, which sets a window version and then checks to see if it already running as such

$g_szVersion = 'ScRiPt'
If WinExists($g_szVersion) Then Exit ; It's already running
AutoItWinSetTitle($g_szVersion)

Is there an equivalent in a .vbs script? Ideally I would like to set a 'unique' szVersion string as above and then detect that string.

Thanks

Share this post


Link to post
Share on other sites



@NDog probably the easiest option, as it sounds like you could be running multiple copies from multiple locations, would be to lay down some specific machine-setting when your script launches, then remove it when it completes. For example, you could create a temp file in the system temp directory, then delete when you are done, something like this:

strFile = "myScript.tmp"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(objFSO.GetSpecialFolder(2) & "\" & strFile)

Set objFile = nothing

You could do the same with a registry entry. Then just have your script check for the existence of that file/registry item on startup. The only thing you'll need to be careful about is a prematurely aborted script, which would leave the file/registry key on the machine. Ensure you're taking every precaution to prevent script crash.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Thanks for the suggestion. Ideally I would like to check something in memory, however you gave me good ideas to consider.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0