Solved

Sync 2 Directories

Posted on 2004-09-15
4
222 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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