0

I have a script that created a scheduled task to run one minute from the time of creating, using the PowerShell line (Get-Date).AddMinutes(1).ToString("HH:mm").

$Time = (Get-Date).AddMinutes(1).ToString("HH:mm")
schtasks /CREATE /S $env:computername /RU $env:username /SC ONCE /TN "!One_time_task" /TR !Task! /ST $Time

The problem is I need to do the same using CMD because I cannot use PowerShell for this task. Currently I have to explicitly edit the line to the desired time, I know time /t is the currently time, is there a way to just add one minute easily?

Indigo
  • 3

1 Answers1

0

You can use %time% to get the current time. Use string extraction for hour and minute separately. Handle hour and day wrap.

SETLOCAL ENABLEDELAYEDEXPANSION
rem avoid hour wrapping while reading %time%
set ttime=%time%
rem current hour
set ch=%ttime:~0,2%
rem current minute
set cm=%ttime:~3,2%
rem next minute
set /a nm=!cm!+1
set nh=!ch!
rem hour overflow?
if !nm! geq 60 (
  set /a nm=!nm!-60
  rem restore leading 0
  set nm=0!nm!
  set /a nh=!nh!+1
)
rem day overflow?
if !nh! geq 24 (
  rem wrap to next day somehow
) 
echo Current time + 1 min = !nh!:!nm!

Beware of %time% localization - you may need to adapt the script depending of your local format. If it needs to run with varying localization it's necessary to populate %ttime% from WMIC or similar, as pointed out in @phuclv's comment.

Zac67
  • 5,130
  • 1
  • 13
  • 22