1

Lazarus & Free Pascal GUI プログラムにいくつかの機能を追加する必要があります。ユーザーが選択したディレクトリから別のディレクトリにファイルをコピーするためにも必要です。ソース ディレクトリの「Choose Source」TSelectDirectoryDialog ボタンの onclick イベントと、宛先ディレクトリの「Choose Destination」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

どうもありがとう

テッド

4

1 に答える 1

1

私はとても幸せで誇りに思っており、初めて自分の質問に答えます! 私はすべてを基本に戻し、他の人のより複雑な例を読むのをやめました (彼らは私を混乱させたからです)。私はLazarus FileUtils Refにリストされている基本的な手順に固執し、これを思いつきました。これは機能します。エラーチェックなどを組み込む必要がありますが、ソースディレクトリを取得して宛先ディレクトリに再構築し、元のディレクトリから宛先にファイルをコピーするコードがあります。完全に Free 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;       
于 2012-02-22T15:27:50.703 に答える