Solved

Sync 2 Directories

Posted on 2004-09-15
4
225 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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