Solved

Copy a directory and sub directory

Posted on 1997-10-07
3
244 Views
Last Modified: 2010-04-04
Whithin a Delphi3 prog, I am using dos xcopy to copy a directory from  my CD Drive to my C drive.

Is there a way to do so from Delphi programmably.
I am a beginner but know how to make or remove directories.
0
Comment
Question by:kennys
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
ID: 1346940
There are several ways of copying files in Delphi.
You could recurse through the directories and copy each file individually, creating subdirectories where needed.  This would require quite a bit of code (using FindFirst, FindNext, CopyFile, MkDir, etc.) and a knowledge of recursion.

An easy way would be to let the Windows 95 shell do the work for you.  You can even get it to pop up a progress bar if it's a long copy operation.  Here is an example:

(Add ShellAPI to your form's uses list!!)

procedure TForm1.Button1Click(Sender: TObject);
var
      FileOp: TSHFileOpStruct;
      sTo, sFrom: array[0..79] of Byte;
begin
      FillChar(FileOp, SizeOf(FileOp), 0);
      FileOp.Wnd := Self.Handle;
      FileOp.wFunc := FO_COPY;
      FillChar(sFrom, SizeOf(sFrom), 0);
      FillChar(sTo, SizeOf(sTo), 0);
      StrPCopy(@sFrom, 'C:\Test\*.*');
      StrPCopy(@sTo, 'C:\Temp');
      FileOp.pFrom := PChar(@sFrom);
      FileOp.pTo := PChar(@sTo);
      FileOp.fFlags := FOF_SIMPLEPROGRESS or FOF_RENAMEONCOLLISION or FOF_NOCONFIRMMKDIR;
      SHFileOperation(FileOp);
end;

This example will copy everything from C:\Test into C:\Temp.  You can change it to copy everything in C:\Test into C:\Temp\Test by changing the following line
      StrPCopy(@sFrom, 'C:\Test\*.*');
to
      StrPCopy(@sFrom, 'C:\Test');

Note that a progress bar will only appear if it is a long copy operation.  Windows won't pop one up for short ops.

Hope this helps.  If you'd prefer an example using FindFirst etc. mail me at davekw@iafrica.com.

JB
0
 

Author Comment

by:kennys
ID: 1346941
I wonder if you can help me again.
You recently gave me the following piece of code to
allow me to copy a directory.
When I try to compile it, I get the following error:-
Incompatable types: 'Array' and 'Char'
in this line>> StrPCopy(@sFrom, 'D:\Test\*.*');
I am using Delphi3 and have ShelAPI in the uses clause.


procedure TForm1.Button1Click(Sender: TObject);
var
FileOp: TSHFileOpStruct;
sTo, sFrom: array[0..79] of Byte;
begin
FillChar(FileOp, SizeOf(FileOp), 0);
FileOp.Wnd := Self.Handle;
FileOp.wFunc := FO_COPY;
FillChar(sFrom, SizeOf(sFrom), 0);
FillChar(sTo, SizeOf(sTo), 0);
StrPCopy(@sFrom, 'D:\Test\*.*');
StrPCopy(@sTo, 'D:\Temp');
FileOp.pFrom := PChar(@sFrom);
FileOp.pTo := PChar(@sTo);
FileOp.fFlags := FOF_SIMPLEPROGRESS or FOF_RENAMEONCOLLISION or FOF_NOCONFIRMMKDIR;
SHFileOperation(FileOp);
end;



0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1346942
Hi again

Under the Project-Options menu, select the "Compiler" tab.  Make sure that "Type @ Operator" is NOT checked.  Click the "Default" checkbox at the bottom of the window to ensure that any future projects have "Typed @ Operator" off too.

JB
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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