?
Solved

Copy a directory to another location.

Posted on 2004-11-06
5
Medium Priority
?
245 Views
Last Modified: 2010-04-05
I need to copy a directory to another location, complete with all it's contents.  All sub-directories (and their sub-directories etc) must also be copied at the same time.

It is also important that the directory must ALWAYS be copied even if the destination directory name already exists, WITHOUT any message to the user. During the course of the program, this directory will be copied many times and I don't want the user continually being asked for confirmation.

I am using Delphi 6. This is URGENT so I am awarding the maximum 500 points for this.  Please supply the code.  Many thanks.
0
Comment
Question by:rincewind666
  • 3
5 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12512805
function  Copy_Dir(Dir, Source, Target: string): Boolean;
var
  B:      Boolean;
  SHFOS:  TSHFileOpStruct;
begin
  B := False;
  try
    Fillchar(SHFOS, SizeOf(SHFOS), #0);
    with SHFOS do
    begin
      Wnd := 0;
      wFunc := FO_COPY;
      pFrom := PChar(Source+Dir + #0#0);
      pTo := PChar(Target+Dir + #0#0);
      fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
    end;
    Application.ProcessMessages;
    B := (SHFileOperation(SHFOS)=0);
  finally
    Result := B;
  end;
end;
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 2000 total points
ID: 12512811
usage:

procedure TForm1.Button_Lan_CopyFolderClick(Sender: TObject);
var
  S:      string;
  T:      string;
  R:      string;
  Dir:    string;
begin
//  S := '\\emo\C:';
  S := 'C:';
  R := 'F:';
  Dir := '\SFX';
  case Copy_Dir(Dir, S, R) of
    True:  T := 'was SUCCESSFULY COPIED to ';
    False: T := 'was NOT SUCCESSFULY COPIED to ';
  end;
  ShowMessage(T);
end;
0
 
LVL 46

Expert Comment

by:aikimark
ID: 12513675
rincewind666,

If you need an asynchronous copy, you can shell an XCOPY command or a batch file (.bat or .cmd) with the XCOPY command.  For large copy operations, this would provide your user with better application response, since the user wouldn't be waiting for the copy operation to complete.

command Example:
XCOPY "sourcepath\*.*" "destinationpath" /y /s /e /q /r

Delphi Example snippet:
Uses
  ShellAPI;

ShellExecute(Form1.Handle, nil, 'xcopy.exe',
  '"sourcepath\*.*" "destinationpath" /y /s /e /q /r', 'c:\windows', SW_HIDE);

==================
Alternatively, you could spawn a thread to run the esoftbg code asynchronously.
0
 

Author Comment

by:rincewind666
ID: 12557316
Many thanks for your help.  Greatly appreciated.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12560599
You are welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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

839 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