Batch file 如何创建触发bat文件的可延迟倒计时消息框

Batch file 如何创建触发bat文件的可延迟倒计时消息框,batch-file,vbscript,messagebox,Batch File,Vbscript,Messagebox,您好,我正在尝试创建一个带有“延迟”按钮的消息框,如果用户未按下延迟按钮,脚本将启动批处理文件(或命令) 所以应该是这样的: 您好用户您的管理员要求您下班后注销此应用程序,我们检测到您仍在使用该程序,如果您仍在计算机上,请按Delay 理想情况下,我希望它能够检测某个程序何时处于活动状态,并且仅当它处于活动状态时才激活 谢谢 这是最终版本 @if (@CodeSection == @Batch) @then @echo off setlocal for /f %%I in ('forfiles

您好,我正在尝试创建一个带有“延迟”按钮的消息框,如果用户未按下延迟按钮,脚本将启动批处理文件(或命令)

所以应该是这样的:

您好用户您的管理员要求您下班后注销此应用程序,我们检测到您仍在使用该程序,如果您仍在计算机上,请按Delay

理想情况下,我希望它能够检测某个程序何时处于活动状态,并且仅当它处于活动状态时才激活

谢谢

这是最终版本

@if (@CodeSection == @Batch) @then
@echo off
setlocal

for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
setlocal
set "task=agent.exe"
set "timeout=120"
rem // Is %task% running?
tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (

    rem // Re-launch script with JScript interpreter
    wscript /e:JScript /nologo "%~f0" %timeout% || (
        rem // If timeout or user hits No, kill %task%
        taskkill /im "%task%" /f
    )
)

rem // End main runtime

goto :EOF

rem // Begin JScript portion
@end
var osh = WSH.CreateObject('WScript.Shell'),
    nag = 'Greetings!  Your administrator has requested you to log out of Touchstar '
        + 'after work.  It appears you are still using it.'
        + ' If you are still here, Press Yes to continue working.\n\n'
        + 'Otherwise, press no to close touchstar.'
        + 'Touchstar will close automatically in less than ' + WSH.Arguments(0) + ' seconds.';
    popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
WSH.Quit(popup - 6);
@如果(@code节==@Batch)@那么
@回音
setlocal
对于/f%%I in('forfiles/p“%~dp0.”/m“%~nx0”/c“cmd/c echo 0x07“),请设置“beep=%I”

设置/P“=%beep%”

挑战:已接受。将此保存为.bat文件并运行它

@如果(@code节==@Batch)@那么
@回音
setlocal
设置“task=cmd.exe”
设置“超时=60”
rem//%task%正在运行吗?
任务列表/fi“imagename eq%task%”查找/i“%task%”NUL&&(
rem//使用JScript解释器重新启动脚本
wscript/e:JScript/nologo“%~f0”%timeout%||(
rem//如果超时或用户点击“否”,则杀死%task%
任务终止/im“%task%”/f
)
)
rem//结束主运行时
后藤:EOF
rem//beginjscript部分
@结束
var osh=WSH.CreateObject('WScript.Shell'),
noise=WSH.CreateObject('WMPlayer.OCX.7'),
“你好!您的管理员已请求您注销此'
+“下班后申请。您似乎仍在使用该程序。如果
+'实际上,您不在计算机上,请忽略此消息。\n\n'
+'否则,按Yes继续工作,或按No继续并关闭'
+"申请。此消息将在'+WSH.Arguments(0)中自毁
+“几秒钟。”;
有(噪音){
URL=osh.Environment('Process')('SYSTEMROOT')+'\\Media\\Windows惊叹号.wav';
控件。play();
}
popup=osh.popup(nag,WSH.Arguments(0),“您还在吗?”,0x4+0x20+0x1000);
退出(弹出-6);
该脚本采用了JosefZ建议的
WshShell.Popup()
方法,非常简单。它使用一个函数在批处理脚本中包含JScript,而不必使用辅助/临时文件。它还用于评估
find
wscript
的退出代码

播放声音的想法来源于WMPlayer.OCX.7


编辑:我在上面看到了您的编辑,以及您在粘贴中加入
^G
的努力。看看这个。您可以捕捉到变量的嘟嘟声,如下所示:

@echo关闭
setlocal
对于/f%%I in('forfiles/p“%~dp0.”/m“%~nx0”/c“cmd/c echo 0x07“),请设置“beep=%I”

设置/P“=%beep%”

挑战:已接受。将此保存为.bat文件并运行它

@如果(@code节==@Batch)@那么
@回音
setlocal
设置“task=cmd.exe”
设置“超时=60”
rem//%task%正在运行吗?
任务列表/fi“imagename eq%task%”查找/i“%task%”NUL&&(
rem//使用JScript解释器重新启动脚本
wscript/e:JScript/nologo“%~f0”%timeout%||(
rem//如果超时或用户点击“否”,则杀死%task%
任务终止/im“%task%”/f
)
)
rem//结束主运行时
后藤:EOF
rem//beginjscript部分
@结束
var osh=WSH.CreateObject('WScript.Shell'),
noise=WSH.CreateObject('WMPlayer.OCX.7'),
“你好!您的管理员已请求您注销此'
+“下班后申请。您似乎仍在使用该程序。如果
+'实际上,您不在计算机上,请忽略此消息。\n\n'
+'否则,按Yes继续工作,或按No继续并关闭'
+"申请。此消息将在'+WSH.Arguments(0)中自毁
+“几秒钟。”;
有(噪音){
URL=osh.Environment('Process')('SYSTEMROOT')+'\\Media\\Windows惊叹号.wav';
控件。play();
}
popup=osh.popup(nag,WSH.Arguments(0),“您还在吗?”,0x4+0x20+0x1000);
退出(弹出-6);
该脚本采用了JosefZ建议的
WshShell.Popup()
方法,非常简单。它使用一个函数在批处理脚本中包含JScript,而不必使用辅助/临时文件。它还用于评估
find
wscript
的退出代码

播放声音的想法来源于WMPlayer.OCX.7


编辑:我在上面看到了您的编辑,以及您在粘贴中加入
^G
的努力。看看这个。您可以捕捉到变量的嘟嘟声,如下所示:

@echo关闭
setlocal
对于/f%%I in('forfiles/p“%~dp0.”/m“%~nx0”/c“cmd/c echo 0x07“),请设置“beep=%I”

设置/P“=%beep%”

将此保存为.bat文件:

@if (@CodeSection == @Batch) @then

@echo off
CScript //nologo //E:JScript "%~F0"
if %errorlevel% equ -1 (
   echo The user NOT confirmed, proceed to auto-destruction!
) else (
   echo Continue normally...
)
pause
goto :EOF

@end

WScript.Quit(WScript.CreateObject("WScript.Shell").Popup(
   "This program will proceed to auto-destruction\nunless you press OK in 10 seconds",
   10,"Popup Title",48))

10是秒数,48是显示的图标类型(本例中为感叹号),但您可以按说明修改它。

将其另存为.bat文件:

@if (@CodeSection == @Batch) @then

@echo off
CScript //nologo //E:JScript "%~F0"
if %errorlevel% equ -1 (
   echo The user NOT confirmed, proceed to auto-destruction!
) else (
   echo Continue normally...
)
pause
goto :EOF

@end

WScript.Quit(WScript.CreateObject("WScript.Shell").Popup(
   "This program will proceed to auto-destruction\nunless you press OK in 10 seconds",
   10,"Popup Title",48))

10是秒数,48是显示的图标类型(本例中为感叹号),但您可以按说明修改它。

下面是纯vbscript中的另一个解决方案,它允许您监视多个应用程序。试一试,你就会明白我的意思

Option Explicit
If AppPrevInstance() Then   
    MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
        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
'**************************************************************************

下面是纯vbscript中的另一个解决方案,它允许您监视多个应用程序。试一试,你就会明白我的意思

Option Explicit
If AppPrevInstance() Then   
    MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
        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
'**************************************************************************

我猜您的批处理正在尝试终止此应用程序或其他内容?总之,您想检查此应用程序是否正在运行,因此您用msgbox“delayed”通知用户点击等待一段时间,如果没有,您是否启动批处理文件终止此应用程序?我是对的还是错的?这可能是一个很好的起点我猜您的批处理正试图终止此应用程序或其他内容?总之,您希望检查此应用程序是否正在运行,因此您使用msgbox“delayed”通知用户点击等待一段时间,如果没有,您将批处理文件启动到k