Solved

recursive copy in delphi 3

Posted on 1998-06-22
24
208 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:jeurk
  • 11
  • 9
  • 2
  • +1
24 Comments
 
LVL 1

Expert Comment

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

Elfrieda :)
0
 
LVL 4

Author Comment

by:jeurk
ID: 1355165
Edited text of question
0
 
LVL 4

Author Comment

by:jeurk
ID: 1355166
Nothing more the copy or move. About what did you thought ?

What is Elfrieda for language ?

Thanks for the fast interest.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1355167
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
 
LVL 4

Author Comment

by:jeurk
ID: 1355168
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355169
The component comes with sources!!! -Remove the dialog if you want! ;-) Or...? Have I got something wrong?
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1355170
PS do you want me to fix it?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1355171
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
 
LVL 4

Author Comment

by:jeurk
ID: 1355172
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
 
LVL 5

Expert Comment

by:ronit051397
ID: 1355173
Then think what you really want and then ask a question.
0
 
LVL 1

Expert Comment

by:michaue
ID: 1355174
What about using the ForceDirectories ...

Elfrieda :)
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1355175
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Author Comment

by:jeurk
ID: 1355176
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355177
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
 
LVL 4

Author Comment

by:jeurk
ID: 1355178
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355179
I think it's not very hard - I'll try to change it so it will work with threads...
0
 
LVL 4

Author Comment

by:jeurk
ID: 1355180
If you do that and it is working like I thought I will increase the points to 150.
Thanks in advance.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1355181
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
 
LVL 4

Author Comment

by:jeurk
ID: 1355182
Hello,
matvey : please post this as an answer, and let's stop the thread here.
Thanks for the help matvey and rony.
Jeurk
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1355183
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355184
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
 
LVL 3

Accepted Solution

by:
Matvey earned 100 total points
ID: 1355185
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355186
I must say taht Greedy was the one who helped me with this so thanks go to him too.
0
 
LVL 4

Author Comment

by:jeurk
ID: 1355187
Thanks Matvey and by the way Greedy.
CU in another thread ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now