0

I need to copy files from one directory to another in time (set by the user like 22:00) automatically. How I have to set my schedule procedure of copy (everyday till manually stopped) to start at time which set up in TTimeEdit?

Here is my code:

var

    ScheduleStart : TTime;

begin

    Timer1.Enabled := false;

    ScheduleStart := (AutoStartTime.Time);

    if ScheduleStart > Now then Timer1.Enabled := true

    else

    begin

        showmessage('Copying is started...');

    end;

end;

Can anybody help me out with this please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marcell Nemeth
  • 97
  • 3
  • 4
  • 13
  • 3
    Why not creating a task in Windows Task Scheduler? Your delphi application can run the command line schtasks to create/delete/change a task. Type schtasks /? on the command line to see syntax. – fpiette Dec 02 '20 at 12:34

1 Answers1

0

I suppose your code works inside an OnTimer event handler of a timer (Timer1). I don't know the Interval of this timer, maybe 1 second. So, when one second has passed after activation of the timer, this event handler is called, but your first line DISABLES the timer. There is also a line to enable the timer again, but this will never be reached when the current time is before the ScheduleStart. To fix do this: Remove the "Timer1.Enabled := false". This way the timer periodically checks whether the ScheduleStart is reached or not. When it is you must deactivate the timer to prevent triggering your copy again again and again. So, I would change your code as follows:

// Code to start the timer
procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := true;
end;

// Code executed regularly while the timer it enabled.
procedure TForm1.Timer1Timer(Sender: TObject);
var 
  ScheduleStart : TTime;
begin
  ScheduleStart := AutoStartTime.Time;
  if Now() >= ScheduleStart then
  begin
    // strop triggering the event any more
    Timer1.Enabled := false;  
    // Execute the process
    ShowMessage('Copying has started...'); 
  end;
end;
wp_1233996
  • 784
  • 1
  • 4
  • 12
  • Thank you very much for your help! Yes the interval is 1 second. The fist line really disabled the timer. I made because the events set up for that completed procedure and all time just run and not wait until the set it time. I still have problem with timer. Is still not start at time witch set it up. What I'm doing wrong? – Marcell Nemeth Dec 17 '20 at 08:30
  • Of course, the Timer's Enabled property must be set to true. When you put a TTimer instance on the form this is done by default - please check the property Enabled in the Object Inspector. If you have to set Enabled to false initially there must be a button/menu item or whatever with which you can set Enabled to true (see the Button1Click procedure in my code). Once Enabled is true my code should work. – wp_1233996 Dec 18 '20 at 09:16