WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

User Sample Code

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Fast AppExist UDF

 Keywords: AppExist udfAppExist Application Exist Fast GetTickCount Process Win32_Process WMI 

The AppExist function can sometimes take a while to confirm the existence of an application. Mainly because it makes multiple attempts to track down the process. Starting in WinBatch 2007B we changed the optional parameters in AppExist:
	AppExist(program-name [, flags [, retries]])
By default, AppExist and AppWaitClose retry up to 7 times (with a half-second delay between tries) before returning, if the specified application doesn't exist. You can override this by specifying a different value for "retries", which may be >= 0 (0 = no retries).


For 2007A and older you can use this WMI code.

#DefineFunction udfAppExist(processname)
   ; Computer name and current scripts process id.
   strComputer = "."
   retvalue = 0
   objWMIService = ObjectGet("winmgmts:{impersonationLevel=impersonate}!\\":strComputer:"\root\cimv2")

   ; Get this script's sessionId
   strQuery = "select * from win32_process where Name = '":processname:"'"
   colProcesses = objWMIService.ExecQuery( strQuery )
   ForEach objProcess In colProcesses
       procid = objProcess.ProcessId
       If procid > 0 Then retvalue = 1
       Break
   Next

   objProcess    = 0
   colProcesses  = 0
   objWMIService = 0
   Return retvalue
#EndFunction

programname = 'notepad.exe'

;Run( programname, "" )
;WinWaitExist('~Notepad', 10)

starttick = GetTickCount()
ret = udfAppExist (programname)
endtick = GetTickCount()
If ret
   Pause('Notice','Application Exists')
Else
   Pause('Notice','Application Does Not Exist')
EndIf
Pause('AppExist test results (in milliseconds)', endtick-starttick)
Exit

Article ID:   W18479
Filename:   Fast AppExist UDF.txt
File Created: 2011:06:02:11:52:22
Last Updated: 2011:06:02:11:52:22