Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sync 2 Directories

Posted on 2004-09-15
4
223 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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