Solved

copying multiple files from a directory to another directory

Posted on 2002-07-30
8
307 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:gandalf_the_white
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks a lot.

best regards
Manuel Lopez (lopem)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
APK file name 7 63
Wininet read php file from internet issue 6 68
FMX enumerated colours 2 70
Downloading email attachments 2 53
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

11 Experts available now in Live!

Get 1:1 Help Now