Solved

Copy a directory and sub directory

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

15 Experts available now in Live!

Get 1:1 Help Now