An alarm program
@echo Off
:alarm
start music.mp3
cls
goto alarm
How to take/parse system time so that I can make it start alarming at every 15 min or every 1 hour? A complete code would be appreciated.
An alarm program
@echo Off
:alarm
start music.mp3
cls
goto alarm
How to take/parse system time so that I can make it start alarming at every 15 min or every 1 hour? A complete code would be appreciated.
You can use this to get the current minutes:
%Time:~3,2%
You should then be able to build your IF statement, as follows:
IF %Time:~3,2%==00 START music.mp3
The only downside is this will keep firing off for the entire duration of the 00 minute.
You could do something like this:
@ECHO OFF
SET HasRun=0
:alarm
IF %Time:~3,2%==00 (
IF %HasRun%==0 (
SET HasRun=1
START music.mp3
PAUSE
)
)
IF %Time:~3,2%==01 (
SET HasRun=0
)
CLS
GOTO alarm
The gist of it is when the minutes = 00 (the first of the hour), we start the MP3, and then let the system know the alarm has run. One minute after the hour, we reset the HasRun flag so that the next hour, the alarm can start again.
It's fairly clunky, though, since the batch file will be constantly grinding away.
The following code will take a timestamp and convert it into a number of minutes since midnight.
REM store current timestamp
set now=%TIME:~0,-6%
REM Break time apart into hour and minute
set hour=%now:~0,-3%
set min=%now:~-2%
REM convert hour and minute into minutes
set /a starttime=((%hour% * 60) + %min%)
To do what you are trying to do, you would use the above code to take a "start" timestamp. Add the desired number of minutes to the result (set /a endtime=(%starttime% + %interval%)
). Now, create a loop that will get the current time, use the above code to convert it into a number of minutes, and then compare it to %endtime%
. If the two match, then the desired number of minutes has elapsed.
Note that this will not behave as expected if the time interval crosses midnight (that is, if the start time is at the end of one day and the end time is at the start of another day).
Of course, the easier way to solve this problem is to use a program like sleep
that was designed to do just this. You can get one from the Windows 2003 Resource Kit (details here) or from the GNU Coreutils for Windows package.
Update:
If what you really want is simply a task scheduler, then use the Task Scheduler that is built into Windows (Control Panel -> Scheduled Tasks, details here or here) or the command line version (the at
or schtasks
commands).