Solved

copying multiple files from a directory to another directory

Posted on 2002-07-30
8
311 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 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
 
LVL 17

Expert Comment

by:geobul
ID: 7230228
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

19 Experts available now in Live!

Get 1:1 Help Now