Solved

copying multiple files from a directory to another directory

Posted on 2002-07-30
8
314 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 17

Expert Comment

by:geobul
ID: 7230228
0
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 303
Delphi XE10, MySQL Query 4 143
Downloading email attachments 2 73
How to Get Images From Server using App Tethering 11 21
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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