拉撒路& Free Pascal - 如何递归地将文件的源目录复制到另一个目录?

时间:2012-02-14 14:22:13

标签: freepascal lazarus file-copying

我需要为Lazarus&添加一些功能。免费的Pascal GUI程序 - 我需要它还将用户选择的目录中的文件复制到另一个目录。我有源目录的“选择源”TSelectDirectoryDialog按钮onclick事件和目标目录的“选择目标”TSelectDirectoryDialog按钮onclick事件。我有一个第3个按钮来执行从源到目标的复制。

到目前为止,我发现CopyFile复制了文件和原始日期属性,但它没有重新创建用户所选源目录的任何子目录的子目录结构。实际上,我试图在其他地方的新目录中复制源目录。

我到目前为止:

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;        

任何人都可以帮我解决如何编码复制子目录及其文件的问题吗?我也在Lazarus论坛上提出了这个问题:Lazarus Thread

非常感谢

泰德

1 个答案:

答案 0 :(得分:1)

我非常高兴和骄傲并且第一次提供了对我自己的问题的回答!我把整个事情都剥夺了基础,并停止阅读其他人的更复杂的例子(因为他们只是困惑我)。我坚持Lazarus FileUtils Ref中列出的基本程序,并提出了这个,这是有效的。我需要构建一些错误检查和东西,但我现在拥有的是获取源目录的代码,在目标目录中重建它,然后使用完全免费的Pascal代码将文件从原始目录复制到目标,并且没有OS特定语法。贴在下面是为了别人的利益。请添加任何具体的评论,以使其更好,更快,更有效。谢谢。

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;