我有一个在计算机上运行并检查某些内容的批处理文件 - Start.bat
我想编写VBS,如果批处理文件正在运行,则在后台检查。如果跑,什么也不做。如果没有,请运行。
此代码总是返回我批处理文件正在运行,但他没有。
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set service = locator.ConnectServer()
Set props = service.ExecQuery _
("select name, description from Win32_Process where name = 'Start.bat'")
num = props.Count
If num > 0 Then
WScript.Echo "Program is running"
Else
WScript.Echo "Program is not running"
End If
Set WSHShell = Nothing
Wscript.Sleep 1000
loop
答案 0 :(得分:0)
尝试一下:
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set service = locator.ConnectServer()
Set props = service.ExecQuery _
("select name, CommandLine, description from Win32_Process where name = 'cmd.exe'")
num = props.Count
If num > 0 Then
WScript.Echo "Program is running"
Exit Do
Else
WScript.Echo "Program is not running"
End If
Set WSHShell = Nothing
Wscript.Sleep 1000
loop