?
Solved

Sync 2 Directories

Posted on 2004-09-15
4
Medium Priority
?
226 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
[X]
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
4 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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