如何使用批处理脚本创建重复的弹出提醒消息?

时间:2015-05-12 13:28:18

标签: batch-file cmd popup scheduled-tasks

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

1 个答案:

答案 0 :(得分:0)

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

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等待,所有这些都在无限循环内。

已修改以适应评论

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

这是我手头的现有代码的复制/粘贴/更改,它包含了传递给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%