Click here to Skip to main content
Click here to Skip to main content

VBScript - Count Instances of a Process

By , 22 Jan 2013
 

Introduction

It can be useful sometimes to know how many instances of a process are running at a time, for instance, to prevent a second instance of your VBScript being triggered. I created a short tip on how to get it to work as desired!

Using the code 

The code is in VBScript. VBScript is processed by "wscript.exe" (runtime compiler). To use it, create a 'New Text Document', rename it to 'myVBScript.vbs'. If you are not able to rename your files onto a file with another file-extension, you first have to enable it. For this go to 'My Computer' or to any open folder and click on it and then select above on the main tab , 'Tools', 'Folder Options...', 'View', and finally uncheck the option called 'Hide extension for known file types'.

Option Explicit
Dim objWMIService, objProcess, colProcess, strComputer, processName, instances

strComputer = "."
instances = 0
processName = "firefox.exe" 

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
If objProcess.Name = processName Then instances = instances + 1
Next

If processName = "wscript.exe" Then 
	If instances = 1 Then
	WScript.Echo "There is no other VBScript running except this one!"
	Else
	WScript.Echo "There are currently " & "(" & instances _
	   & ") " & "VBScript" & " Instances running!"
	End If
Else
	WScript.Echo "There are currently " & "(" & instances & ") " & _
	        """" & processName & """" & " Instances running!"
End If

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

bEGI23
Macedonia, The Republic Of Macedonia, The Republic Of
Member
Programming is my new hobby which i discovered few months ago.
I am ethnic Albanian from Macedonia, i speak the German language which i do master as my first language. Now i'm in process to learn mastering the English language. God willing!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 22 Jan 2013
Article Copyright 2012 by bEGI23
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid