【问题标题】:How to create a repeated popup reminder mesage with batch script?如何使用批处理脚本创建重复弹出提醒消息?
【发布时间】:2015-07-23 09:55:57
【问题描述】:

是否可以使用批处理脚本创建弹出提醒消息,每隔一小时重复一次弹出消息? 这是一个长批处理脚本的一部分,我不能使用任何其他实用程序或脚本。它必须是批处理脚本。

【问题讨论】:

  • 批处理不能做弹窗。请改用 vbscript。它在技术上不是第三方,因为它是随 Windows 一起安装的。
  • 我不能使用 vb 脚本,因为这是长批处理脚本的一部分。
  • npocmaka 对胜利的建议。mshta "javascript:alert('hi!');close()"
  • 这个脚本一次。但是我该如何重复呢?

标签: batch-file cmd popup scheduled-tasks


【解决方案1】:

如果只需要每小时弹出一条消息,请在批处理文件中包含以下行

start "" /b cmd /q /e /c">nul 2>&1 (for /l %%a in (0) do (msg console this is the message & ping -n 3601 localhost ))"

这将生成一个背景 cmd 实例,该实例将使用 msg 显示弹出窗口并使用 ping 等待,所有这些都在一个无限循环中。

编辑以适应 cmets

正如我所说,处理它的最好方法可能是将消息发送留给调度程序,并从脚本或命令行控制任务

这是我手头现有代码的复制/粘贴/更改,它包含传递给schtasks 的参数。基本思路是

a) 创建一个任务来发送消息

popup.cmd create taskName "this is the message to send each hour"

b) 可以在需要时启用/禁用弹出任务

popup.cmd disable taskName
popup.cmd enable taskName

c) 不需要时,可以删除任务

popup.cmd remove taskName
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Where the tasks will be created
    set "tasksFolder=PopUpTasks"

    rem Get the task name from arguments
    call :getTaskName "%~2" taskName

    rem Check list of arguments 
    set "command="
    for %%a in ( 
        create list remove enable disable status run 
    ) do if not defined command if /i "%%~a"=="%~1" (
        set "command=%%a"
        rem List and creation commands have different arguments
        if "%%~a"=="create" ( 
            call :create "%tasksFolder%\%taskName%" "%~3"
        ) else if "%%~a"=="list" ( 
            call :list "%tasksFolder%"
        ) else ( 
            rem The rest of the commands require a valid task name
            rem so we first test and if everything is OK, continue

            call :checkTaskExist "%tasksFolder%\%taskName%" && (
                call :%%~a "%tasksFolder%\%taskName%"
            ) || (
                call :noTask "%tasksFolder%\%taskName%"
            )
        )
    )

    rem If something failed or if we did not found a valid command, show usage
    if errorlevel 1         call :showUsage
    if not defined command  call :showUsage

    goto :eof

:create taskName message
    if "%~2"=="" exit /b 1
    schtasks /create /f /tn "%~1" /sc HOURLY /mo 1 /np /tr "msg.exe console \"%~2\""
    goto :eof

:remove taskName
    schtasks /delete /f /tn "%~1" 
    goto :eof

:enable taskName
    schtasks /change /enable /tn "%~1" 
    goto :eof

:disable taskName
    schtasks /change /disable /tn "%~1" 
    goto :eof

:status taskName
    schtasks /query /tn "%~1" 
    goto :eof

:run taskName 
    schtasks /run /tn "%~1" 
    goto :eof

:list tasksFolder
    schtasks /query /v /fo list /tn "%~1\\"
    goto :eof

:getTaskName taskName returnVar
    setlocal enableextensions disabledelayedexpansion
    set "taskName=%~1"
    if "%~1"=="" for /f "tokens=1-2 delims=:" %%a in ("%__cd__:\=.%") do set "taskName=%%a.%%b.%~n0"
    endlocal & set "%~2=%taskName%"
    goto :eof

:checkTaskExist taskName
    >nul 2>nul schtasks /query /tn "%~1" 
    exit /b %errorlevel%

:noTask
    echo(
    echo(ERROR: The requested task ["%~1"] does not exist
    exit /b 1

:showUsage
    (
        echo(
        echo( Usage:
        echo(
        echo(  %~n0 COMMAND ["taskName"] ["message"]
        echo(
        echo( With COMMAND :
        echo(    CREATE  "taskName" "message"
        echo(    REMOVE  "taskName" 
        echo(    ENABLE  "taskName" 
        echo(    DISABLE "taskName"
        echo(    STATUS  "taskName"
        echo(    RUN     "taskName"
        echo(    LIST
        echo( 
    ) & exit /b %errorlevel%

【讨论】:

  • 此时出现错误 C:\Users\Plbn>%%a 出乎意料。
  • @AriDey,代码用于批处理文件。在命令行中,将 %%a 替换为 %a
  • 它可以工作,但是当我关闭 cmd 窗口时它无法正常工作,如果是我的脚本,则无法保持 cmd 窗口打开。那么是否可以关闭cmd窗口仍然运行脚本呢?
  • @AriDey,对不起,该代码旨在用于正在运行的批处理文件中。在新指示的场景下,我认为创建计划任务以每小时执行msg 命令并根据需要使用脚本或命令来启用/禁用任务会更容易。
  • 即使我需要此代码用于批处理文件......但唯一的问题是,当我运行代码时,它会保持 cmd 窗口打开。是否可以在不保持 cmd 打开的情况下运行代码?
猜你喜欢
  • 2012-09-12
  • 1970-01-01
  • 2014-04-10
  • 2019-08-10
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多