choice是选择语句,具体语法另外再讲。今天利用它来实现定时执行功能。废话少说直接上代码:
示例一:
@echo off
for %%a in (我 是 一 个 中 国 人) do ping -n 2 127.0|set /p b=%%a
for /d %%a in (我 是 一 个 中 国 人) do set /p b=%%a<nul&ping -n 1 127.1>nul
::自注set后面的%%a如果添加空格,效果是:我 是 一 个,不加空格,效果是:我是一个 @echo off&setlocal enabledelayedexpansion
set "str=熊仔其人"
for /l %%a in (0 1 50) do (
if "!str:~%%a,1!" neq "" (
set/p=!str:~%%a,1!<nul
for /l %%i in (1 1 400) do echo.>nul
) else (
pause>nul&exit/b
)
)
示例二:
@echo off
::指定时间点(格式:2019-08-14 16:17:00)
set TargetTimePoint="2019-08-15 19:05:20" :myTimer
:: 等待1秒执行一次
choice /t 1 /d y /n >nul
::echo 当前时间%date% %time%
::echo 标准输出%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
set NowTime="%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,8%"
echo %NowTime%
if %NowTime% == %TargetTimePoint% (
echo 时间到!!!
goto myTimerExec
) else (
goto myTimer
) :myTimerExec
(echo 终于等到你
echo 5秒后自动关闭
)|msg %username% /time:5 rem goto TurnToB :TurnToA
echo 神清气爽 :TurnToB
echo 超凡脱俗 pause>nul
未完待续