tags:

views:

34

answers:

2

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.

A: 

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.

LittleBobbyTables
in the above code what does 00 indicate, is it 00 for hour , min or secs
subanki
The 00 indicates the minutes, so you could do 15, 30, 45, etc. and handle quarter intervals.
LittleBobbyTables
Thanks mate , %Time:~3,2%==00 , why 3 and 2 ??? ,(or please put comments in code)
subanki
%TIME% returns HH:NN:SS.mm format. ~3,2 tells us to start at the third character, and read 2 characters to the right. If the time were 12:00:01.25, we'd get "00".
LittleBobbyTables
A: 

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).

bta