【问题标题】:How do I create a Delayable count down msg box, which triggers bat file如何创建可触发 bat 文件的可延迟倒计时消息框
【发布时间】:2015-02-05 18:12:10
【问题描述】:

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

所以它应该是这样的:

您好用户您的管理员要求您在下班后退出此应用程序,我们检测到您仍在使用该程序,如果您仍在您的机器上,请按延迟。

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

谢谢

这是最终版本

@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);

感谢所有帮助创建此内容的人(非常感谢 Rojo)以及所有回答的人。

  • 登斯拉特

【问题讨论】:

  • 我猜你的批次正试图杀死这个应用程序或其他什么?
  • 总之,你想检查这个应用程序是否正在运行,所以你用一个“延迟”的消息框通知用户等待一段时间,如果没有,你启动批处理文件杀死这个应用程序 ?我是对还是错?
  • Popup method 可能是一个很好的起点

标签: batch-file vbscript messagebox


【解决方案1】:

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

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

@echo off
setlocal

set "task=cmd.exe"
set "timeout=60"

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'),
    noise = WSH.CreateObject('WMPlayer.OCX.7'),
    nag = 'Greetings!  Your administrator has requested you to log out of this '
        + 'application after work.  It appears you are still using the program.  If '
        + 'you are, in fact, not at your computer, please ignore this message.\n\n'
        + 'Otherwise, press Yes to continue working, or No to go ahead and close the '
        + 'application.  This message will self-destruct in ' + WSH.Arguments(0)
        + ' seconds.';

with (noise) {
    URL = osh.Environment('Process')('SYSTEMROOT') + '\\Media\\Windows Exclamation.wav';
    Controls.play();
}

popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);

WSH.Quit(popup - 6);

该脚本采用 JosefZ 建议的 WshShell.Popup() 方法,并且非常简单。它使用hybrid convention 在批处理脚本中包含 JScript,而无需使用辅助/临时文件。它还使用conditional execution 来评估findwscript 的退出代码。

WMPlayer.OCX.7 播放声音的想法来自this answer


编辑:我看到了您在上面的编辑以及您在粘贴中包含^G 的努力。看一下这个。您可以像这样捕捉到变量的哔哔声:

@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

... 将发出 3 次蜂鸣声而不回显新行。

【讨论】:

  • 谢谢你,Rojo,效果很好,我的老板也笑了:D
  • 我们必须对这项业务有一种奇思妙想的感觉,以免发疯。 :)
  • 确实,你用这个真的救了我的培根,我们有一个程序记录用户使用它的时间,但为了确保他们“或没有”迟到,他们必须注销,他们倾向于让自己保持登录状态。
  • 另一个问题,你能让它在弹出窗口时发出窗口错误声音吗?
  • @Denslat 添加。任何进一步的新功能请求都应该作为一个单独的问题提出。但是请尝试表明您在询问之前已经付出了一些努力来研究解决方案。欢迎来到 S.O.!
【解决方案2】:

将此保存为 .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 是显示的图标类型(在这种情况下是感叹号),但您可以按照here 的说明进行修改。

【讨论】:

    【解决方案3】:

    这是另一种纯 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
    '**************************************************************************
    

    【讨论】:

    • 运行一次成功。我点击“取消”,脚本似乎退出了。现在随后的尝试导致“有一个现有的程序!\n'%test.vbs%''你还应该修改 Popup() 最终参数以包含 0x1000 位以始终在顶部。(vbModal?只是一个猜测。)
    • 因为我们必须只启动一次这个脚本,它会循环运行并尝试打开 Internet Explorer 或另一个批处理文件,例如位于 c:\toto1.bat 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2019-02-17
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多