Solved

Copy a directory and sub directory

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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