批处理文件:从用户输入创建多个具有多个开始时间的计划任务

时间:2015-02-11 22:57:08

标签: batch-file scheduled-tasks

这是我想要完成的事情:

我有一个批处理文件,可以创建2个所需的计划任务。 它将提示用户根据需要输入开始时间。 使用输入的时间,它将为其创建的每个计划任务创建%StartTime%。

我需要创建第二个任务来实际创建1分钟后开始时间,即用户输入的内容。

实施例: 我运行脚本并输入15:00的开始时间。我希望第二项任务从15:01开始,而不是将这段时间用于这两项任务。

代码:

set /p startTime=Enter Start Time in 24 hour format (ex 14:00 for 2pm, 08:00 for 8am): 
schtasks /create /tn "Task1" /sc DAILY /st %StartTime% /tr "Task 1"
schtasks /create /tn "Task2" /sc DAILY /st %StartTime% /tr "Task 2"

对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

set /p startTime=Enter Start Time in 24 hour format (ex 14:00 for 2pm, 08:00 for 8am): 
for /F "tokens=1-3 delims=:." %%a in ("%startTime%") do (
   set timeHour=%%a
   set timeMinute=%%b
   set timeSeconds=%%c
)
rem Convert HH:MM to minutes + 1
set /A newTime=timeHour*60 + timeMinute + 1
rem Convert new time back to HH:MM
set /A timeHour=newTime/60, timeMinute=newTime%%60
rem Adjust new hour and minute
if %timeHour% lss 10 set timeHour=0%timeHour%
if %timeMinute% lss 10 set timeMinute=0%timeMinute%

schtasks /create /tn "Task1" /sc DAILY /st %StartTime% /tr "Task 1"
schtasks /create /tn "Task2" /sc DAILY /st %timeHour%:%timeMinute% /tr "Task 2"

在使用JosefZ提供的链接后,我想出了这段代码的变体,非常感谢你指出我正确的方向。

答案 1 :(得分:0)

这是一个几乎防弹解决方案,现在对用户的输入进行了基本的有效性测试:

@ECHO OFF >NUL
:loopStartTime
set "startTime=%time:~0,5%"
echo Need  Task Start Time in 24 hour format 
echo  (example 14:00 for 2pm, 08:00 for 8am)
set /p "startTime=Please enter Start Time (default=%StartTime: =%) "
::: test user's input and force repeat in case of extra serious error
::: e.g. mistyped `09/00` or `09%00` or `09<00` etc. instead of `09:00`
set "_val_test=%startTime: =%"
set /A "_val_test=1%_val_test::=+%" || goto :loopStartTime

::: format startTime to become a valid valid time in HH:mm (24 hours) pattern
call :addminutes startTime "%startTime: =%" 0
::: previous call with "quoted" 2nd parameter as user's input might contain
::: a valid parameter delimiter, i.e. one or more from commonly used 
::: Comma `,` Semicolon `;` Equals `=` Space ` ` Tab `     `   
echo schtasks /create /tn "Task1" /sc DAILY /st %StartTime% /tr "Task 1"

call :addminutes startTim2 %startTime% 1
echo schtasks /create /tn "Task2" /sc DAILY /st %StartTim2% /tr "Task 2"

goto :eof
:addminutes
:::::::::::::::::::::::::::::::::::::::::::::::::::
::: %1 (byref, obligatory) output variable name
::: %2 (byval, optional) valid time HH:mm (24 hours)
::: %3 (byval, optional) minutes to add  
:::::::::::::::::::::::::::::::::::::::::::::::::::
SETLOCAL enableextensions disabledelayedexpansion
set "sTime=%~2"
set /A "sPlus=100%~3%%100" 2>nul
for /F "tokens=1-2 delims=:. " %%a in ("%sTime%") do (
   set /A "timeHour=100%%a%%100" 2>nul
   set /A "timeMinute=100%%b%%100" 2>nul
)
::: Convert HH:MM to minutes + %3
set /A "newTime=(timeHour*60)+timeMinute+sPlus"
::: Convert new time back to HH:MM
set /A "timeHour=newTime/60%%24"
set /A "timeMinute=newTime%%60"
::: Adjust new hour, minute and return value
set "timeHour=00%timeHour%"
set "timeMinute=00%timeMinute%"
set "sTime=%timeHour:~-2%:%timeMinute:~-2%"
::: do not change next line
ENDLOCAL&set "%~1=%sTime%"&goto :eof

请注意。用户可以输入几乎任何内容以响应set /P;来自:addminutes过程的输出总是以HH:mm(24小时)模式产生有效时间,例如

  • <Enter>结果到当前时间,参见初始set "startTime=%time:~0,5%"
  • 15结果为15:00
  • 9:9结果为09:09
  • any nonsens结果为00:00

修改:代码中的更多评论

:addminutes程序基于Aacini&#39; s answer