Search code examples
timebatch-filecommandtimed

Batch: Trying to get a command to execute at a specific amount of seconds


This works for hours, running in a loop as soon as it hits 13 hours it executes.

@ECHO OFF

:time
echo %time%
FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~0,2%") DO SET /a HH=%%b
IF %HH% equ 13 goto success
goto time

:success
echo success finally
pause
goto time

but if I run this for seconds in a loop for it to execute at 13 seconds it just keeps running in a loop and doesn't execute the command.

@ECHO OFF

:time
echo %time%
FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~5,2%") DO SET /a SS=%%b
IF %SS% equ 13 goto success
goto time

:success
echo success finally
pause
goto time

This Doesn't Work either

FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~6,2%") DO SET /a SS=%%b

I need this to execute at any specified amount of seconds. So that instead of for instance executing at 13:00:00.00. I can get it to execute at 13:00:13.00 or something like that.


Solution

  • Needs to be a 6 not a 5 for the offset.

    %time:~6,2%
    

    It does not have to be complex. This works, but unless you provide more details as to what you are trying to accomplish we cannot provide the best help.

    :time
    echo %time%
    IF %time:~6,2% equ 13 goto success
    goto time
    

    Note that using the time environment variable is user locale specific.

    Use something like for no locale format time.

    wmic path win32_operatingsystem get LocalDateTime