Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copy a directory and sub directory

Posted on 1997-10-07
3
Medium Priority
?
258 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

916 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