Solved

Copy a directory and sub directory

Posted on 1997-10-07
3
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

13 Experts available now in Live!

Get 1:1 Help Now