Solved

copying multiple files from a directory to another directory

Posted on 2002-07-30
8
329 Views
Last Modified: 2010-04-04
Here's what I have:

The user supplies a directory, usrDir.

There may be anywhere from none to thousands of files in that directory. I want to copy everything that is in there to progDir.  How do I do this?

chmod101
0
Comment
Question by:chmod101
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Accepted Solution

by:
lopem earned 100 total points
ID: 7188642
I wrote myself long time ago a program to copy a folder. It can copy also subdirectories inside the directory you want to copy.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FindFile, Filectrl, ShellApi, ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    FindFile1: TFindFile;
    Image1: TImage;
    Image2: TImage;
    ListBox1: TListBox;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1 : TForm1;
  Path,
  Path2 : string;

implementation

{$R *.DFM}


procedure CopyRecursive(srcDir, dstDir: string);

var
    cmd : string;    
begin
  cmd:='/c xcopy '+srcDir+' '+dstDir+' /E';
  ShellExecute(Application.handle,'open','command',PChar(cmd),nil,SW_SHOW);
end;

procedure TForm1.Button2Click(Sender: TObject);

var
    Salida  : TextFile;
    AppPath : string;
begin
  AppPath := ExtractFilePath(Application.ExeName);
  AssignFile(Salida,AppPath+'\'+'Copia.ini');
  Rewrite(Salida);
  Writeln(Salida,Path);
  Writeln(Salida,Path2);
  CloseFile(Salida);
  Application.Terminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ((Path='') or (Path2='')) then
  begin
    ShowMessage('Error... No hay directorio Source/Target');
//no source directory
  end
  else
  begin
    Path := Edit1.Text;
    Path2 := Edit2.Text;
    if FileExists(Path2) then CopyRecursive(Path,Path2)
    else
    begin
      MkDir(Path2);
      CopyRecursive(Path,Path2);
    end;
  end;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
  Path := ExtractFilePath(Application.ExeName);
  SelectDirectory(Path, [sdAllowCreate,sdPerformCreate,sdPrompt],1000);
  FindFile1.Path := Path;
  ListBox1.Items := FindFile1.SearchForFiles;
  Form1.Edit1.text := Path;
end;

procedure TForm1.Image2Click(Sender: TObject);
begin
  Path2 := ExtractFilePath(Application.ExeName);
  SelectDirectory(Path2, [sdAllowCreate,sdPerformCreate,sdPrompt],1000);
  FindFile1.Path := Path2;
  ListBox1.Items := FindFile1.SearchForFiles;
  Form1.Edit2.text := Path2;
end;

procedure TForm1.FormCreate(Sender: TObject);

var
    Entrada  : TextFile;
    AppPath  : string;
    InpSt,
    OutSt    : string;
begin
  AppPath := ExtractFilePath(Application.ExeName);
  if FileExists(AppPath+'\'+'Copia.ini') then
  begin
    AssignFile(Entrada, AppPath+'\'+'Copia.ini');
    reset(Entrada);
    readln(Entrada, InpSt);
    readln(Entrada, OutSt);
    Edit1.Text := InpSt;
    Edit2.Text := OutSt;
    Path := InpSt;
    Path2 := OutSt;
    CloseFile(Entrada);
  end
  else
  begin
    Edit1.Text := '';
    Edit2.Text := '';
    Path := '';
    Path2 := '';
  end;
end;

end.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7190044
Hi,

Another version:

uses ShellApi;

// copy a directory tree
procedure CopyDir(FromDir, ToDir : string);
var
 FOS :tSHFileOpStruct;
begin
  FromDir := Trim(FromDir);
  if FromDir[Length(FromDir)] <> '\' then FromDir := FromDir + '\';
  ToDir := Trim(ToDir);
  with FOS do
  begin
    Wnd := Application.Handle;
    wFunc := FO_COPY;
    pFrom := PChar(FromDir + '*.*' + #0);
    pTo := PChar(ToDir + #0);
    fFlags := FOF_ALLOWUNDO or FOF_MULTIDESTFILES or FOF_NOCONFIRMMKDIR;
  end;
  SHFileOperation(FOS);
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyDir('c:\tools','d:\tools');
end;

Regards, Geo
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 7230221
that is really strange

i get no help for the tSHFileOpStruct.

but it compiles without an error so the compiler knows that data structure.

how or where do you access help on that structure?
(i looked in the sdk help but found nothing)

gandalf

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 7231124
thanks for answering so fast

how do you find this kind of information?
i mean if the help system doesn't tell me how am
i supposed to use this command?

and what about the other (thousands?) useful commands
that do exist and i do not use because the help doesn't
do what it is supposed to do?
is it necessary to read alle the microsoft knowledge
base articles to get this kind of information or
do you have other resources that are not so time consuming?

gandalf

0
 
LVL 17

Expert Comment

by:geobul
ID: 7231165
1. Asking questions here ;-)

2. exploring pas files from Delphi\Source directory (vcl and rtl\win). I'm using Delphi 5.

3. examining projects from Delphi\Demos directory.

4. msdn, Delphi related sites, etc.

Regards, Geo
0
 

Author Comment

by:chmod101
ID: 7353224
Honestly, I don't need this answer any more, but I just wanted to clear this question. Thanks for the help, everyone.
0
 
LVL 3

Expert Comment

by:lopem
ID: 7353247
thanks a lot.

best regards
Manuel Lopez (lopem)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…

688 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