recursive copy in delphi 3

Hi,
In one hand I have a list containing directory names and file names.
On the other hand I have a destination directory.
I would like to have source code that shows me how to copy/move the files
and the dirs recursively to the dest directory.
Any hint someone ?
Thanks in advance.
Jeurk
LVL 4
jeurkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

michaueCommented:
Do you simply want to copy the files from one directory structure to another, or something more specific ???

Elfrieda :)
0
jeurkAuthor Commented:
Edited text of question
0
jeurkAuthor Commented:
Nothing more the copy or move. About what did you thought ?

What is Elfrieda for language ?

Thanks for the fast interest.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

MatveyCommented:
This is the first component I found, and it works well and cool. There is a dozen more in the Delphi pages, though not all of them can copy folders recursivly.

http://bes.trendline.co.il/torry/vcl/filedrv/epflyingop.zip

G` luck!
bosism@netvision.net.il
0
jeurkAuthor Commented:
Sorry I reject it cause I'd like to have a code solution without showing a dialog.
I will handle the visual aspect by myself.
Thanks anyway
0
MatveyCommented:
The component comes with sources!!! -Remove the dialog if you want! ;-) Or...? Have I got something wrong?
0
MatveyCommented:
PS do you want me to fix it?
0
ronit051397Commented:
uses ShellApi;

type
TMoveCopy=(fiCopy, fiMove);

Procedure CopyMoveFiles(MoveCopy: TMoveCopy; Fromdir, ToDir: String);
Var
  OpStruct:TSHFileOpStruct;
Begin
  If (Trim(Fromdir)='') or (Trim(Todir)='') Then Exit;
  AppendStr(FromDir, #0);
  AppendStr(ToDir, #0);
  With OpStruct Do
  Begin
    Wnd:=0;
    if MoveCopy=fiCopy then
      wFunc:=FO_COPY
    else if MoveCopy=fiMove then
      wFunc:=FO_MOVE;
    pFrom:= @FromDir[1];
    pTo:= @ToDir[1];
    fFlags:=FOF_ALLOWUNDO or FOF_SILENT or FOF_NOCONFIRMATION;
  End;
  SHFileOperation(OpStruct);
End;

0
jeurkAuthor Commented:
Hello guys,
thanks for the help.
I think that your answers were equivalent and that you Matvey answered it first.
I have to think about what I want really.
I think that I would have prefered a solution that is not using SHFileOperation.
Let's say that I have my copymove function.
Simply give me source code that walks trough the dirtree of the given directory something
like that:

function copy the whole directory structure to destdir(sourceDir, destDir : string);
begin
       walk to the sourceDir dirTree and treat each file or dir after another;
end;

procedure cmProc(sourceLst : tstringlist, destDir :string);
begin
        for each entry in sourceLst do
        if it's a file copy it to destDir
        else
        if it's a directory copy the whole directory structure to destdir
end

What do you think ?
0
ronit051397Commented:
Then think what you really want and then ask a question.
0
michaueCommented:
What about using the ForceDirectories ...

Elfrieda :)
0
MatveyCommented:
So, do you want a fast copy routine and a procedure that will walk throught directorys and copy file by file? Do I get you right?

I don't really understand what for eather. Why don't you like the component? - I really want to know why...
0
jeurkAuthor Commented:
I apologize.
If you the points for your work, I won't mind giving them to you.
Just let me know.
When you ask a question, sometimes you think you're clear, but you're not.
That's life.

0
MatveyCommented:
I don't really need the points - after all we're all here to help each other. Instead can you just explain clearer why the component doesn't do what you want and what you really want it to do.

We're all trying to help you, of course not beyond your will. If you'd be just a bit clearer I'm sure we could do better...
0
jeurkAuthor Commented:
Hello
it's because I want to make multi thread copys
I'm making a program on which you can drop files and dirs, and then it let's you copy them where you want.
So i need to be able to cancel the think when I want, and i want to have access to the filename strings etc...

0
MatveyCommented:
I think it's not very hard - I'll try to change it so it will work with threads...
0
jeurkAuthor Commented:
If you do that and it is working like I thought I will increase the points to 150.
Thanks in advance.
0
MatveyCommented:
I've found some time and wrote this. I think it can help you, but though it's a TThread descendant it doesn't works like one. Maybe someone can find the bug?

unit Unit3;

interface
uses
  Windows, Classes, SysUtils;

type
  TFCopy = class(TThread)
  private
    FFrom: string;
    FMask: String;
    FTo: string;
  public
    procedure CopyFiles(path, mask, destpath : AnsiString; Recursive: Boolean);
    procedure Execute; override;
  published
    property FilesFrom: string read FFrom write FFrom;
    property FilesTo: string read FTo write FTo;
    property Mask: string read FMask write FMask;
  end;

implementation

{$I-}
procedure TFCopy.CopyFiles(path, mask, destpath : AnsiString; Recursive: Boolean);
var
  srRes : TSearchRec;
  iFound : Integer;
begin
  if path[Length(path)] <> '\' then path := path +'\';
  if destpath[Length(destpath)] <> '\' then destpath := destpath +'\';

  MKDir(destpath);

  if recursive then
    begin
    iFound := FindFirst( path + '*.*', faAnyfile, srRes );
    while iFound = 0 do
      begin
      if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) then
        if srRes.Attr and faDirectory > 0 then
          CopyFiles(path + srRes.Name, mask, destpath + srRes.Name, Recursive);
      iFound := FindNext(srRes);
      end;
    FindClose(srRes);
    end;

  iFound := FindFirst(path+mask, faAnyFile-faDirectory, srRes);
  while iFound = 0 do
  begin
    if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) and ( srRes.Name <> '' ) then
      CopyFile(PChar(path+srRes.Name), PChar(destpath+srRes.Name), False);
    iFound := FindNext(srRes);
  end;
  FindClose( srRes );
end;

procedure TFCopy.Execute;
begin
  CopyFiles(FFrom, mask, FTo, True);
end;

end.
0
jeurkAuthor Commented:
Hello,
matvey : please post this as an answer, and let's stop the thread here.
Thanks for the help matvey and rony.
Jeurk
0
MatveyCommented:
I know it's working, but it's not working as a thread. Wait, I'll try to ask a new question to find the bug, I'm just very curious myself. I'm sure it'll be answered shortly so you can fully enjoy this... (Might be even today)
0
MatveyCommented:
Still didn't find the problem, but I found something for scanning the directories:

http://bes.trendline.co.il/torry/vcl/packs/delphi3lib.zip

-TTreeScan - have a look
0
MatveyCommented:
OK, it works as a thread now:

unit UThread;

interface

uses
  Classes, Dialogs, Windows, SysUtils;

type
  FileCopy = class(TThread)
  private
    FFrom: String;
    FTo: string;
    FMask: string;
    FRecursiveCopy: Boolean;
    procedure CopyFiles(path, mask, destpath : AnsiString; Recursive: Boolean);
  protected
    procedure Execute; override;
  Published
    Property FromDir: String read FFrom write FFrom;
    Property ToDir: String read FTo write FTo;
    Property Mask: String read FMask write FMask;
    property RecursiveCopy: Boolean read FRecursiveCopy write FRecursiveCopy;
  end;

implementation

{$I-}
procedure FileCopy.CopyFiles(path, mask, destpath : AnsiString; Recursive: Boolean);
var
  srRes : TSearchRec;
  iFound : Integer;
begin
  if path[Length(path)] <> '\' then path := path +'\';
  if destpath[Length(destpath)] <> '\' then destpath := destpath +'\';

  MKDir(destpath);

  if recursive then
  begin
    iFound := FindFirst( path + '*.*', faAnyfile, srRes );
    while iFound = 0 do
    begin
      if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) then
        if srRes.Attr and faDirectory > 0 then
          CopyFiles(path + srRes.Name, mask, destpath + srRes.Name, Recursive);
      iFound := FindNext(srRes);
    end;
    FindClose(srRes);
  end;

  iFound := FindFirst(path+mask, faAnyFile-faDirectory, srRes);
  while iFound = 0 do
  begin
    if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) and ( srRes.Name <> '' ) then
      CopyFile(PChar(path+srRes.Name), PChar(destpath+srRes.Name), False);
    iFound := FindNext(srRes);
  end;
  FindClose( srRes );
end;

procedure FileCopy.Execute;
begin
  CopyFiles(FFrom, FMask, FTo, FRecursiveCopy);
end;

end.



You activate the thing like this:

var
  MyThread: FileCopy;
begin
MyThread := FileCopy.Create(True);
MyThread.FreeOnTerminate := True;
MyThread.FromDir := Form1.DirectoryListBox1.Directory;
MyThread.Mask := Form1.FileListBox1.Mask;
MyThread.ToDir := Form1.DirectoryListBox2.Directory;
Mythread.RecursiveCopy := Form1.CheckBox4.Checked;
MyThread.Resume;
end;

Hope this is finally what you need. Tell me what do you think...-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MatveyCommented:
I must say taht Greedy was the one who helped me with this so thanks go to him too.
0
jeurkAuthor Commented:
Thanks Matvey and by the way Greedy.
CU in another thread ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.