Re: copy directories and files

Posted by Jenny on January

In Reply to: Make a CD to place directories on the G: disk posted by john

To create a directory in one step, creating all in-between directories, use the Delphi function ForceDirectories.

Because Delphi does not have routines for copying files nor directories, use the Windows API CopyFile function to copy one file:

CopyFile(PointerToSourceName, PointerToDestinationName, DoNotOverwrite)

If the parameter DoNotOverwrite is True, an existing file will not be overwritten. So, use False if you want to copy in any case.
If the function succeeds, it returns True.

Example for copying some files from drive F: to drive C:

SourceDir := 'f:\';
DestDir := 'c:\program files\photo';
ForceDirectories(DestDir);
FileName := 'a.dll';
CopyFile(PChar(SourceDir+'\'+FileName), PChar(DestDir+'\'+FileName), False);
FileName := 'b.dll';
CopyFile(PChar(SourceDir+'\'+FileName), PChar(DestDir+'\'+FileName), False);
SourceDir := 'f:\examples';
DestDir := 'c:\program files\photo\examples';
ForceDirectories(DestDir);
FileName := 'a.jpg';
CopyFile(PChar(SourceDir+'\'+FileName), PChar(DestDir+'\'+FileName), False);
FileName := 'b.jpg';
CopyFile(PChar(SourceDir+'\'+FileName), PChar(DestDir+'\'+FileName), False);

If not to many files have to be copied, you can use the above code to copy each file.
But if you want to use wildcards like *.dll or *.* then you have to make a list of the files of each source directory with FindFirst and FindNext. For an example, see the article:
"Find files recursively" at   www.festra.com/eng/snip04.htm

Related Articles and Replies