Solved

Sync 2 Directories

Posted on 2004-09-15
4
220 Views
Last Modified: 2010-04-05

Hi !
I have an directory with a large amount of video files. I would like to  backup this
directory to an external USB hardware disk. The program must not overwrite or copy already
existing files. The files never change. After a while some of the source files and directories
will be deleted. This files and directories shall be deleted on the backup disk then, too.
Does anybody has a finished code snippet for that already ?

Thanks
fuluppi
0
Comment
Question by:fuluppi
4 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 500 total points
ID: 12070981
check  code below:

unit mainform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    EditSrcDir: TEdit;
    EditBackupDir: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    fl:TStringList;
            procedure backupfile(src,des:string);
            procedure CopyDir(src,des:string);
            procedure DeleteDir(src:string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
      backupfile(EditsrcDir.Text,EditBackupDir.Text);
end;

procedure TForm1.CopyDir(src,des:string);
var
  sr: TSearchRec;
  FileAttrs: Integer;
begin
      if (not FileExists(des)) then CreateDir(des);

  FileAttrs := faArchive;
  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            CopyFile(pchar(src+'\'+sr.Name),pchar(des+'\'+sr.Name),true);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  FileAttrs := faDirectory;
  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Name='.') or (sr.name='..') then continue;
          if (sr.Attr and FileAttrs) = sr.Attr then
            CopyDir(src+'\'+sr.Name,des+'\'+sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

procedure TForm1.DeleteDir(src:string);
var
  sr: TSearchRec;
  FileAttrs: Integer;
begin
  FileAttrs := faDirectory;
  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Name='.') or (sr.name='..') then continue;
          if (sr.Attr and FileAttrs) = sr.Attr then
            DeleteDir(src+'\'+sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  FileAttrs := faArchive;
  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            DeleteFile(src+'\'+sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  RemoveDir(src);
end;

procedure TForm1.backupfile(src,des:string);
Var
      flsrc,fldes:TStringList;
  sr: TSearchRec;
  FileAttrs: Integer;
  i,j:integer;
begin
      flsrc:=TStringList.Create;
  fldes:=TStringList.Create;
  FileAttrs := faArchive;
  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            flsrc.Add(sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  if FindFirst(des+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            fldes.Add(sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  flsrc.Sort;
  fldes.Sort;

  i:=0;
  j:=0;
  while((i<flsrc.Count)and (j<fldes.Count)) do
  begin
        if (flsrc[i]<fldes[j]) then
    begin
          CopyFile(pchar(src+'\'+flsrc[i]),pchar(des+'\'+flsrc[i]),true);
      Inc(i);
    end
    else if (flsrc[i]>fldes[j]) then
    begin
          DeleteFile(des+'\'+fldes[i]);
      Inc(j);
    end
    else
    begin
          Inc(i);
      Inc(j);
    end;
  end;
  while(i<flsrc.Count) do
  begin
         CopyFile(pchar(src+'\'+flsrc[i]),pchar(des+'\'+flsrc[i]),true);
          Inc(i);
  end;
  while(j<fldes.Count) do
  begin
          DeleteFile(des+'\'+fldes[j]);
      Inc(j);
  end;

//--------------------
      flsrc.Clear;
  fldes.Clear;
  FileAttrs := faDirectory;

  if FindFirst(src+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            flsrc.Add(sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  if FindFirst(des+'\*.*', FileAttrs, sr) = 0 then
  begin
    repeat
          if (sr.Attr and FileAttrs) = sr.Attr then
            fldes.Add(sr.Name);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  flsrc.Sort;
  fldes.Sort;


  i:=0;
  j:=0;
  while((i<flsrc.Count)and (j<fldes.Count)) do
  begin
        if ((flsrc[i]='.') or (flsrc[i]='..')) then
    begin
          inc(i);
           continue;
    end;
        if ((fldes[j]='.') or (fldes[j]='..')) then
    begin
          inc(j);
           continue;
    end;

        if (flsrc[i]<fldes[j]) then
    begin
      CopyDir(src+'\'+flsrc[i],des+'\'+flsrc[i]);
      Inc(i);
    end
    else if (flsrc[i]>fldes[j]) then
    begin
          DeleteDir(des+'\'+fldes[i]);
      Inc(j);
    end
    else
    begin
          BackupFile(src+'\'+flsrc[i],des+'\'+flsrc[i]);
          Inc(i);
      Inc(j);
    end;
  end;
  while(i<flsrc.Count) do
  begin
        if ((flsrc[i]='.') or (flsrc[i]='..')) then
    begin
          inc(i);
           continue;
    end;
    CopyDir(src+'\'+flsrc[i],des+'\'+flsrc[i]);
         Inc(i);
  end;
  while(j<fldes.Count) do
  begin
        if ((fldes[j]='.') or (fldes[j]='..')) then
    begin
          inc(j);
           continue;
    end;
        DeleteDir(des+'\'+fldes[j]);
    Inc(j);
  end;

end;
end.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now