0

I want to put a whole number (these numbers will always represent minutes) and that adds to the current time to use it in the SCHTASKS command. Example if it is currently 14:56:03 i put 9 or 09 in var1 should set 15:05:03 in var2. I have this candy

@echo OFF
echo add minutes
set/p "var1=>"
set/a "var2=%var1%+%time%"
SCHTASKS /change /sd %date% /st %var2% /tn task

I understand that this does not work but I do not know how to do it. Any ideas?

  • 2
    Use PowerShell, even from your batch file if necessary. _It has methods of adding/subtracting from the time_. – Compo Nov 14 '18 at 11:15

2 Answers2

3

I combined the methods posted at this answer in order to complete this request:

@echo OFF
echo add minutes to %time%
set/p "var1=>"

for /F "tokens=1-3 delims=:.," %%a in ("%time%") do set /A "new=(%%a*60+1%%b-100+var1)*60+1%%c-100"
set /A "ss=new%%60+100,new/=60,mm=new%%60+100,hh=new/60,hh-=hh*!(hh-24)"
set "var2=%hh%:%mm:~1%:%ss:~1%"

ECHO SCHTASKS /change /sd %date% /st %var2% /tn task

EDIT: Change to next day added

The next code also uses the method described at this answer to adjust the date part to the next day when necessary:

@echo OFF
setlocal

echo add minutes
set/p "var1=>"

for /F "tokens=2 delims==" %%a in ('wmic OS Get LocalDateTime /value') do set "dt=%%a"
set /A "D=%dt:~0,8%, MM=1%dt:~4,2%-100, newS=((1%dt:~8,2%-100)*60+1%dt:~10,2%-100+var1)*60+1%dt:~12,2%-100"
set /A "s=newS%%60+100,newS/=60,m=newS%%60+100,h=newS/60,newDD=h/24,h%%=24"
if %newDD% neq 0 (
   set /A "newMM=!( 1%D:~6%-(130+(MM+MM/8)%%2+!(MM-2)*(!(%D:~0,4%%%4)-2)) ), lastMM=!(1%D:~4,2%-112), newYYYY=newMM*lastMM"
   set /A "MM+=newMM*(1-lastMM*12), D+=newYYYY*(20100-1%D:~4%) + !newYYYY*newMM*(100*MM+10000-1%D:~4%) + newDD"
)
set "var2=%h%:%m:~1%:%s:~1%"  &  set "var3=%D:~6,2%/%D:~4,2%/%D:~0,4%"

ECHO SCHTASKS /change /sd %var3% /st %var2% /tn task
Aacini
  • 59,374
  • 12
  • 63
  • 94
1

As the entered minute offset could also change the date, I'd let PowerShell do the calculation

@echo off
set /p "var1=enter minutes to add: "
for /f "tokens=1*" %%A in ('
  powershell -NoP -C "(Get-Date).AddMinutes(%var1%).ToString('yyyy/MM/dd HH:mm:ss')"
') do (
  Set "MyDate=%%A"
  set "MyTime=%%B"
)
echo SCHTASKS /change /sd %MyDate% /st %MyTime% /tn task
  • Format of the date dd/MM/yyyy to work with schtasks. This solution is the one I like the most precisely because it also solves the day although I did not mention it in the question. Thank you :D – Technomancer Nov 14 '18 at 15:57
  • I do not understand why this always set 00 seconds in the tasks ... example in the echo mytimes shows 18:03:22 and in the task has put 18:03:00 – Technomancer Nov 14 '18 at 17:05
  • May be an issue with schtasks itself, the help `schtasks /change /?` just mentions hours:minutes with `/st` –  Nov 14 '18 at 17:13
  • That's what I thought :( – Technomancer Nov 14 '18 at 18:03