Reputation: 33
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?
Upvotes: 1
Views: 3620
Reputation: 67206
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
Upvotes: 5
Reputation:
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
Upvotes: 1