?
Solved

copying multiple files from a directory to another directory

Posted on 2002-07-30
8
Medium Priority
?
346 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Accepted Solution

by:
lopem earned 400 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
Technology Partners: 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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