我非常高兴和自豪,并第一次为我自己的问题提供答案!我把整个事情都剥离了基础,不再阅读其他人的更复杂的例子(因为他们只是让我感到困惑)。我坚持使用Lazarus FileUtils Ref中列出的基本程序并想出了这个,它很有效。我需要构建一些错误检查和东西,但我现在拥有的是获取源目录的代码,在目标目录中重建它,然后将文件从原始目录复制到目标,使用完全免费的 Pascal 代码并且没有操作系统特定的语法。为了他人的利益,粘贴在下面。请添加任何建设性的评论,以使其更好、更快、更高效。谢谢。
procedure TForm1.Button3Click(Sender: TObject);
begin
ProcessDir(SourceDir);
end;
procedure TForm1.ProcessDir(const SourceDirName: string);
var
NoOfFilesFoundInSourceDir, i, NoOfFilesCopiedOK : integer;
FilesFoundToCopy : TStringList;
SourceDirectoryAndFileName, SubDirStructure, FinalisedDestDir, FinalisedFileName : string;
begin
Memo1.Lines.Clear;
SubDirStructure := '';
FinalisedDestDir := '';
NoOfFilesFoundInSourceDir := 0;
NoOfFilesCopiedOK := 0;
// Ensures the selected source directory is set as the directory to be searched
// and then fina all the files and directories within, storing as a StringList.
SetCurrentDir(SourceDirName);
FilesFoundToCopy := FindAllFiles(SourceDirName, '*', True);
NoOfFilesFoundInSourceDir := FilesFoundToCopy.Count;
try
for i := 0 to FilesFoundToCopy.Count -1 do
begin
Memo1.Lines.Add('File Index : '+IntToStr(i)+' File Name: '+FilesFoundToCopy.Strings[i]);
SourceDirectoryAndFileName := ChompPathDelim(CleanAndExpandDirectory(FilesFoundToCopy.Strings[i]));
// Determine the source sub-dir structure, from selected dir downwards
SubDirStructure := IncludeTrailingPathDelimiter(ExtractFileDir(SourceDirectoryAndFileName));
// Now concatenate the original sub directory to the destination directory and form the total path, inc filename
// Note : Only directories containing files will be recreated in destination. Empty dirs are skipped.
// Zero byte files are copied, though, even if the directory contains just one zero byte file.
FinalisedDestDir := DestDir+SubDirStructure;
FinalisedFileName := ExtractFileName(FilesFoundToCopy.Strings[i]);
// Now create the destination directory structure, if it is not yet created. If it exists, just copy the file.
if not DirPathExists(FinalisedDestDir) then
begin
if not ForceDirectories(FinalisedDestDir) then
begin
ShowMessage(FinalisedDestDir+' cannot be created.');
end;
end;
// Now copy the files to the destination dir
if not FileUtil.CopyFile(SourceDirectoryAndFileName, FinalisedDestDir+FinalisedFileName, true) then
begin
ShowMessage('Failed to copy file : ' + SourceDirectoryAndFileName)
end
else
NoOfFilesCopiedOK := NoOfFilesCopiedOK + 1;
end;
finally
FilesFoundToCopy.free;
end;
ShowMessage('Total files copied OK : ' + IntToStr(NoOfFilesCopiedOK));
end;