Copy a directory to another location.

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.
rincewind666Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

esoftbgCommented:
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
esoftbgCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
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
rincewind666Author Commented:
Many thanks for your help.  Greatly appreciated.
0
esoftbgCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.