# Comparing a variable to the system time in a batch file



## brett30 (Jan 13, 2009)

Hi, 
I am trying to write a batch file in Windows xp that will get the system time store it in a variable then compare that to a constant time such as 10:10 PM. Then, if they are equal it will run the rest of the code, but until it is equal it needs to keep checking. Is there like while or do while loops? I am a total noob in batch file writing. Also, Is there a way to get that batch file to run even if the computer is not logged in? For example the computer is sitting at the log on screen and the right time comes around so the batch file runs and does its thing anyways?
Thanks for any help in advance.


----------



## TheOutcaste (Aug 8, 2007)

This sounds more like something you should use Scheduled Tasks for. It can be set to run a batch file when the user is not logged on.

If you have to do it in a batch file, use the built in *%time%* variable. On my system the format is HH:MM:SS.SS. Regional settings may affect this, so type *Echo %time%* in a command prompt to see how it's displayed on yours. You'll need to parse the hours and minutes before doing the check.
You can also use *now*, but that includes Month, Date, Year and possibly Day of week as well, so would need even more parsing.

There are no while or do loops in batch, so you would need to constantly loop, which will load the CPU, or add a delay before checking again.
Two ways to add a delay are to use ping, or use the sleep.exe utility, part of the Windows 2003 Resource Kit.

This will do what you want. I've included two wait routines, as there are two ways to use ping to create a delay. To use the 2nd, change the call statements from *Call :_wait* to *Call :_wait2*.
If you use sleep.exe, just replace *call :_wait* with *sleep*
If you run the batch, then log off the account, it will exit.
If you add it to the HKLM run key, it should start at boot.
You might be able to keep it running using the *at* command to start it.
I've not tested either method.

```
@echo off
Set _hour=20
Set _min=10
:: If you want to specify the time on the command line, use this instead:
:: Set _hour=%1
:: Set _min=%2
:: And call using name h m
:: Do not include a leading zero in the hour or minute
Call :_now
If %_nowh% GEQ %_hour% If %_nowm% GTR %_min% Goto _missed
:_hloop
Call :_now
If %_nowh%==%_hour% Goto _mloop
:: Calculate # of seconds to the end of the hour
Set /a _wait=(60-_nowm)*60
If %_wait% LEQ 60 Goto _mloop
:_MissedEntryPoint
Call :_wait %_wait%
Goto _hloop
:_mloop
Call :_now
If %_nowm%==%_min% Goto _main
:: Calculate # of seconds to the desired minute
Set /a _wait=(_min-_nowm)*60-_nows
Call :_wait %_wait%
Goto _mloop
:_main
Echo It's now time to run the rest of the code
:: Place main code here
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_now
Set _nowh=%time:~0,2%
Set _nowm=%time:~3,2%
Set _nows=%time:~6,2%
If %_nowh:~0,1%==0 Set _nowh=%_nowh:~-1%
If %_nowm:~0,1%==0 Set _nowm=%_nowm:~-1%
If %_nows:~0,1%==0 Set _nows=%_nowm:~-1%
Goto :EOF
:_wait
Setlocal
Set /a _delay=%1*1000
>Nul ping 1.1.1.1 -n 1 -w %_delay%
Endlocal
Goto :EOF
:_wait2
Setlocal
>Nul ping localhost -n %1
Endlocal
Goto :EOF
:_missed
Echo Current time is %time:~0,5%, Desired time is %_hour%:%_min%
:_missed1
Set /p _response=Do you wish to Abort, Execute now, or Wait 24 hours (A/E/W)?
If /I "%_response%"=="E" Goto _main
If /I "%_response%"=="A" Goto :EOF
If /I NOT "%_response%"=="W" Goto :_missed1
Set /a _wait=(61-%_nowm%)*60
Goto _MissedEntryPoint
```


----------



## brett30 (Jan 13, 2009)

Thank you that helps alot


----------

