Printer Version Table of Contents Project Home Page
.EXAMPLE.....: 63 Run a program at a specified time later in the day
.CATEGORY....: examples
.DISCUSSION..:
This batch file can be used to run a program at a specified time during the day.

In general, this is probably not a good use of Fdate, because this batch file involves a lot of disk activity, which occurs because DOS re-reads the batch file from disk every time it does a GOTO LOOPTOP. You can avoid all this disk activity by running the batch file from a RAM DISK. This was quite a common solution in the days of DOS, but has pretty much fallen by the wayside now. For this reason, you're probably better off using a proper task-scheduler program, some of which are available free on the Web.

But if all else fails, it is indeed possible to use Fdate to do this.

This first batch file uses Fdate's /V feature to put the result into an environment variable. (If you are running in an environment where /V doesn't work, see below.) Note that I have included a display of second#, so you could see that the batch file is looping properly -- you can see the seconds ticking by! You'll want to remove this if you decide to use the batch file from some real use.

.CODE........:
@echo off
:: GET CURRENT ABSOLUTE MINUTE AND PUT IN ENVIRONMENT VARIABLE RUNTIME
FDATE /Ff /At /Ominute#  /vRUNTIME


:: ADD 120 MINUTES (2 HOURS) TO ENVIRONMENT VARIABLE RUNTIME
FDATE /F#add /A%RunTime% /B120  /vRUNTIME


:: LOOP UNTIL NOWTIME HAS REACHED RUNTIME
:LoopTop
  FDATE /Ff /At  /Ominute#   /vNOWTIME
  Fdate /Ff /Osecond#
  FDATE /F#comp  /A%NowTime% /B%RunTime% /vTIMECOMP
  if (%TimeComp%)==(LT) goto LoopTop
:LoopEnd

echo STARTING EXECUTION OF APPLICATION: [program name]

.DISCUSSION..:
If you are running on a system that doesn't support the /V parm, you can use the following code.

The problem with this technique is that it is constantly writing a new copy of TEMP.BAT to your hard disk. A lot of wear and tear on your hard disk. Back in the days when DOSausaurs ruled the earth, and people routinely used virtual disks, that wasn't a problem. But nowadays (September, 2001) I wouldn't use it except as a last resort.

.CODE........:
@echo off
::  GET CURRENT ABSOLUTE MINUTE AND PUT IN ENVIRONMENT VARIABLE RUNTIME
FDATE /Ff /At /Ominute#  /p"set RUNTIME=" > temp.bat
call temp.bat

::  ADD 120 MINUTES (2 HOURS) TO ENVIRONMENT VARIABLE RUNTIME
FDATE /F#add /A%RunTime% /B120  /p"set RUNTIME=" > temp.bat
call temp.bat

::  LOOP UNTIL NOWTIME HAS REACHED RUNTIME
:LoopTop
  FDATE /Ff /At  /Ominute#   /p"set NOWTIME=" > temp.bat
  call temp.bat

  FDATE /F#comp  /A%NowTime% /B%RunTime% /p"set TIMECOMP=" > temp.bat
  call temp.bat
  if (%TimeComp%)==(LT) goto LoopTop
:LoopEnd

:: cleanup
del temp.bat

echo STARTING EXECUTION OF APPLICATION: [program name]