I need to add some functionality to my Lazarus & Free Pascal GUI program - I need it to also copy files from a users chosen dir to another dir. I have a "Choose Source" TSelectDirectoryDialog button onclick event for the source directory and a "Choose Destination" TSelectDirectoryDialog button onclick event for the destination dir. I have a 3rd button to do the copying from Source to Destination.
So far, I have found CopyFile that copies the files and the original date attributes, but it doesn't recreate the subdirectory structure of any subdirectories of the users chosen source directory. I am, in effect, trying to replicate the source directory in a new directory elsewhere.
I have got this far :
Public Vars :
DestDir, SourceDir : string
...
FS := TFileSearcher.Create;
FS.OnFileFound := @CopyTheFile; // CopyTheFile is my own procedure
FS.Search(SourceDir, '*', True);
...
procedure TForm1.CopyTheFile(FileIterator: TFileIterator);
var
DestinationName: String;
begin
DestinationName := IncludeTrailingPathDelimiter(DestDir) + ExtractFileName(FileIterator.FileName);
if not FileUtil.CopyFile(FileIterator.FileName, DestinationName, true) then
ShowMessage(FileIterator.FileName + ' failed to copy');
end;
Can anyone help me with how to code in copying of subdirectories and their files? I have also asked the question at the Lazarus forum here : Lazarus Thread
Many thanks
Ted
I am VERY HAPPY AND PROUD and to provide, for the first time, an answer to my own question! I stripped the whole thing back to basics and stopped reading other people's more complex examples (because they just confused me). I stuck to the basic procedures listed at Lazarus FileUtils Ref and came up with this, which works. I need to build in some error checking and stuff, but what I now have is code that takes the source directory, rebuilds it in the destination directory and then copies the files from the originating directory to the destination, using entirely Free Pascal code and no OS specific syntax. Pasted below for benefit of others. Please add any contructive comments to make it better, faster, more efficient. Thanks.