vbs系统后台持续检测一个进程是否存在

回答百度知道问题:
http://zhidao.baidu.com/question/713784585787480605

问题:
vbs,系统后台持续检测一个进程是否存在,如:qq.exe。如果存在,倒计时X秒将其关闭,如果不存在,继续后台检测。检测时间间隔可设置。

Const INTERVA = 5  '检测间隔
dim ps,ps_str
Set objShell = createobject("wscript.shell")
 
REM 解决CMD黑色闪运行
REM 参考<a href="http://demon.tw/programming/vbs-run-and-exec.html?replytocom=1928" target="_blank">http://demon.tw/programming/vbs-run-and-exec.html?replytocom=1928</a>
host = WScript.FullName
If LCase(Right(host, 11)) = "wscript.exe" Then
    objShell.run "cscript """ & WScript.ScriptFullName & chr(34), 0
    WScript.Quit
End If
REM -----------------------------------------------------------------------
 
do while true
    Set ps = objShell.Exec("tasklist")
    ps_str = ps.Stdout.ReadAll
    'msgbox ps_str
    if find_yn(ps_str) then
        objShell.Popup "5秒后将关闭 QQ !", 5, "Close QQ"
        Set ps = objShell.Exec("taskkill /F /IM QQ.exe /T")  '测试程序
    end if
    wscript.sleep 1000 * INTERVA  '检测间隔
loop
set ps = nothing
set objShell = nothing
 
 
Function find_yn(str_s)   '查找进程是否存在,不存在返回0
    Dim re,ms,m
    find_yn = 0
    set re = New RegExp
    re.Global = True
    re.MultiLine = True 
    're.Pattern = "^(QQa|QQb|QQ).exe"     '可以检测查看多个进程
    re.Pattern = "^QQ.exe"  '测试查找进程
    set ms = re.Execute(str_s)
    For Each m In ms
        'msgbox m
        find_yn = find_yn + 1
    Next
End Function

 

发表回复