42766

vbscript how can you see if a program is open then close it

Question:

How can I use vbscript to determine if a program is running or open and, if it is, close a different program?

For example, how could I check to see if game.exe is running and, if it is, then close google.exe?

Answer1:

Here is an example that show you how to check a process by name and if the check is positive so it kill it.

For example i choose the Game as <strong>Calc.exe</strong> and if the script found it, it will be killed, and also for <strong>internet explorer</strong> and <strong>Google Chrome</strong>

Option Explicit If AppPrevInstance() Then MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !" WScript.Quit Else Dim Game,Browser1,Browser2 Game = "%windir%\system32\calc.exe" Browser1 = "%ProgramFiles%\Google\Chrome\Application\chrome.exe" Browser2 = "%ProgramFiles%\Internet Explorer\iexplore.exe" Do Call Main(Array(Game,Browser1,Browser2)) Call Pause(1) 'Sleeping for 1 minute Loop End If '************************************************************************** Sub Main(colProcessPaths) Dim ProcessPath For Each ProcessPath In colProcessPaths CheckProcess(ProcessPath) Next End Sub '************************************************************************** Sub CheckProcess(ProcessPath) On error resume Next Dim Process,objWMIService,colProcesses,wshShell,btn,Timeout,User Dim ProcessName : ProcessName = StripProcPath(ProcessPath) Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\.\root\cimv2") Set colProcesses = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName)) For Each Process in colProcesses If colProcesses.Count > 0 Then Set wshShell = CreateObject("WScript.Shell") User = CreateObject("WScript.Network").UserName Timeout = 30 'Call the Popup method with a 30 seconds timeout. btn = WshShell.Popup("Hello "& DblQuote(User) & " !" & vbcr &_ "Your Administrator has requested you to log out of this application after work. " & vbcr &_ "We have detected you are still using the program : "& DblQuote(ProcessName) & vbcr &_ "Please press on cancel button if you are still at your machine ?",Timeout,"Question", vbOKCancel + vbQuestion) Select Case btn ' Yes button pressed. case 1 Process.Terminate(0) ' No button pressed. case 2 Exit Sub ' Timed out. case -1 Process.Terminate(0) End Select Else Exit Sub End if Next End Sub '************************************************************************** Function AppPrevInstance() With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2") With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _ " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'") AppPrevInstance = (.Count > 1) End With End With End Function '************************************************************************** Sub Pause(Minutes) Wscript.Sleep(Minutes*1000*60) End Sub '************************************************************************** Function StripProcPath(ProcessPath) Dim arrStr : arrStr = Split(ProcessPath, "\") StripProcPath = arrStr(UBound(arrStr)) End Function '************************************************************************** Function CommandLineLike(ProcessPath) ProcessPath = Replace(ProcessPath, "\", "\\") CommandLineLike = "'%" & ProcessPath & "%'" End Function '************************************************************************** Function DblQuote(Str) DblQuote = Chr(34) & Str & Chr(34) End Function '**************************************************************************

Recommend

  • jQuery Isotope library with HTML tables
  • How to install mysql from batch file?
  • Cannot open include file 'boost/any.hpp' - Invalid argument in VS 2010
  • unzip file using 7z in powershell
  • Python Gui programming and cpu usage. How does one create an 'idle' or 'throttle'
  • How to quit application in AppDomain.CurrentDomain.UnhandledException handler and still ensure that
  • Find and copy code
  • zend smtp mail crashes after 100+ mails
  • Remove parenthesis and text in a file using Python
  • Python: I fell in an infinite loop
  • Pygame the rectangle is not moving?
  • How can I do some cleanup right before an Android app is quitting?
  • Continuous movement of a box in pygame
  • QT Start program as regular user
  • ImportError: cannot import name COMError in python
  • SSLRead() return error -9806/15958)
  • VBScript to check for open process by user
  • data.table replicate rows after join?
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • Call a macro with parameters : Python win32com API
  • How to create a custom Powershell operator?
  • pygame.init() shows as undefined variable after installing Pygame
  • cd command not working with execvp
  • Application bar icon text length
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • Problems with toDataURL HTML5 other ways to get canvas data?
  • npm 5.4.1 install/uninstall all failing
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • wxPython: displaying multiple widgets in same frame
  • Eclipse CDT error: Unable to compile
  • Is playing sound in Javascript performance heavy?
  • App restarts from wrong activity
  • Marklogic : Query response time is very high
  • Chrome doesn't support silverlight anymore? How to solve this?
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Javascript convert timezone issue
  • How to include full .NET prerequisite for Wix Burn installer
  • Is it possible to post an object from jquery to bottle.py?