0

Here is what I am trying to accomplish:

I have a batch file that will create the 2 needed scheduled tasks. It will prompt the user to enter a start time as required. Using that entered time, it will create the %StartTime% for each scheduled task it created.

I need to have the 2nd task that is created to actually create a start time that is 1 minute later that what is entered by the user.

Example: I run the script and enter a time of 15:00 for the start time. Instead of placing this time for both tasks, I want the 2nd task to start at 15:01.

Code:

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"

Any help with this would be greatly appreciated.

4
  • Answered here Commented Feb 11, 2015 at 23:13
  • The answer there is close, I need it to work off of the user input for the original time, that is the part I am having trouble with. I appreciate the response. Commented Feb 11, 2015 at 23:21
  • Just instead of "%time%" referenced there use "%startTime%" and omit that seconds. Commented Feb 11, 2015 at 23:26
  • JosefZ - Thanks for the assistance, with a little more changes, that seemed to help get me in the right direction. Commented Feb 12, 2015 at 0:47

2 Answers 2

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"

After using the link provided by JosefZ, I came up with this variation of that code, thank you so much for pointing me in the right direction.

Sign up to request clarification or add additional context in comments.

Comments

0

Here's a nearly bullet-proof solution, now with a basic validity test of the user's input:

@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

Note. An user can input almost anything in response to set /P; output from the :addminutes procedure always results to a valid time in HH:mm (24 hours) pattern, e.g.

  • <Enter> results to current time, cf. initial set "startTime=%time:~0,5%"
  • 15 results to 15:00
  • 9:9 results to 09:09
  • any nonsens results to 00:00

Edit: more comments in the code

:addminutes procedure based on Aacini's answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.