Count multiple running processes using VB - syntax question

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mmorrison
Newbie
Posts: 13
Joined: Thu Apr 14, 2005 7:15 pm
Location: Arkansas

Count multiple running processes using VB - syntax question

Post by mmorrison » Wed May 11, 2005 10:39 pm

I'm weak on VB syntax - a little help would be great:

I have compiled macros named TestProg-something, TestProg-another, TestProg-xyz, etc. When each is run, I want to ensure none of the others are still active - if they are, I will simply exit from the new macro without doing anything.

The code below (from a support post) normally displays a count of how many times a specified process is running. However, instead of providing a complete process name, I want to use only the 1st 8 characters (TestProg*) - thus my attempt at Left$(Name,8). The idea is, any TestProg* count over 1 means one of my tools is already running...

Is this a syntax issue, or a limitation of the specific procedure call?
--------------------------------
Modified code from support http://www.mjtnet.com/forum/viewtopic.php?t=1652
------------------------------------------------------------------------
VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where LEFT$(Name,8)='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("TestProg"),res
MessageModal>res
---------------------------------------------

Using only Name (no Left$), it works fine. However, my Left$(Name,8) results in:

SWbemObjectSet :-2147217385
Invalid Query
Line 7, Column 1

Ok, somebody make me feel stupid already!

Thanks for any ideas... ~Marvin~

multifix
Newbie
Posts: 8
Joined: Mon Mar 28, 2005 4:17 pm
Location: Florida

Post by multifix » Thu May 12, 2005 11:51 am

Does Macro Scheduler have a LEFT$() function? Did not see that one listed. I think you have to use the MID function. Somebody please correct me if this not the case.
:?
Kris

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu May 12, 2005 12:48 pm

From the VBScript Language Reference (RTFM :wink: )

Left Function
Returns a specified number of characters from the left side of a string.

Left(string, length)

Arguments
string

String expression from which the leftmost characters are returned. If string contains Null, Null is returned.

length

Numeric expression indicating how many characters to return. If 0, a zero-length string("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

Remarks
To determine the number of characters in string, use the Len function.

The following example uses the Left function to return the first three characters of MyString:

Dim MyString, LeftString
MyString = "VBSCript"
LeftString = Left(MyString, 3) ' LeftString contains "VBS".
Note The LeftB function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.

Requirements
Version 1

See Also
Len Function | Mid Function | Right Function
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu May 12, 2005 2:50 pm

Change LEFT$ to LEFT ?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

mmorrison
Newbie
Posts: 13
Joined: Thu Apr 14, 2005 7:15 pm
Location: Arkansas

Post by mmorrison » Thu May 12, 2005 10:28 pm

Thanks for the replies, but Left(Name,8) still gave the same results. I dug a little further, and made it work by using the LIKE operative , instead of "= Left(x,y)".

The sample code is shown below. I hope this is helpful to others

SUPPORT - consider if this snippet (derived from your previous efforts) would be worthy of being placed with http://www.mjtnet.com/forum/viewtopic.php?t=1652. I'm not looking for recognition, I just wish it had been there when I started looking!

To understand the purpose of this code segment:

I have a series of compiled macros that are well coded for timeouts, error checking and error reporting (via dialog screens). Each performs a specific function, then closes - error checking and timeouts prevent a macro from getting 'hung' for more than a few seconds at any point. However, I would get a major system hang if a second macro was started before the first was finished.

This code snippet is at the beginning of each compiled macro, and the name of each executable file begins with "TestTools". When a macro is called, it first checks to see if there is more than one occurance of a TestTools process running (If>%res%>1), in which case, there is a double-beep, and the second macro exits with no action taken - initial code is not interrupted or disturbed, and system is never locked.

With the code as written below, I have fired up 5-10 instances of a compiled macro in rapid succession, yet Windows remained responsive, and everything cleared within a few seconds - NO HANGS!!!

------------Beginning of code sample-------------------
----------------start of TestTools-OneOfTen-------------
VBSTART
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name LIKE '"&ProcessName&"'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("TestTools%"),res
If>%res%>1
pla>c:\TestTools\Error.wav
pla>c:\TestTools\Error.wav
GOTO>Endit
endif
------------body of code goes here-------------
------------end of body--------------------------
Label>Endit
------------end of code sample-----------------

Thanks for a great product, and a great forum... ~Marvin~

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri May 13, 2005 7:26 am

Ah - you were wanting SQL syntax, not VBScript! Sorry I thought you were after VBScript syntax.

It would be difficult to provide examples showing every possible SQL scenario for all available RDBMSs. There is a point at which the developer has to read the docs of the environment they are working with.
MJT Net Support
[email protected]

mmorrison
Newbie
Posts: 13
Joined: Thu Apr 14, 2005 7:15 pm
Location: Arkansas

Post by mmorrison » Thu May 19, 2005 2:45 am

True - I didn't think about that distinction (SQL -vs- VB) when I initially posted. And, I usually do my digging, but it was one of those frustrating days... I guess I was hoping to avoid actual effort! :roll:

Thanks for your help.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts