频道分类

Delphi 如何将多个文件复制到一个

作者:admin 来源: 日期:2020/9/15 13:38:24 人气: 标签:

 
Answer 1
procedure TForm1.Button1Click(Sender: TObject);
var
  Stream1, Stream2: TFileStream;
begin
  Stream1 := TFileStream.Create('c:\file4', fmCreate or fmShareExclusive);
  try
    { first file }
    Stream2 := TFileStream.Create('c:\file1', fmOpenRead or fmShareDenyNone);
    try
      Stream1.CopyFrom(Stream2, Stream2.Size);
    finally
      Stream2.Free;
    end;
    { next file }
    Stream2 := TFileStream.Create('c:\file2', fmOpenRead or fmShareDenyNone);
    try
      Stream1.CopyFrom(Stream2, Stream2.Size);
    finally
      Stream2.Free;
    end;
    { and so on }
  finally
    Stream1.Free;
  end;
end;
Tip by Finn Tolderlund

Answer 2
function AppendFiles(Files: TStrings; const DestFile: string): integer;
var
  srcFS, destFS: TFileStream;
  i: integer;
  F: string;
begin
  result := 0;
  if (Files.Count > 0) and (DestFile <> '') then
  begin
    destFS := TFileStream.Create(DestFile, fmCreate or fmShareExclusive);
    try
      i := 0;
      while i < Files.Count do
      begin
        F := Files(i);
        Inc(i);
        if (CompareText(F, DestFile) <> 0) and (F <> '') then
        begin
          srcFS := TFileStream.Create(F, fmOpenRead or fmShareDenyWrite);
          try
            if destFS.CopyFrom(srcFS, 0) = srcFS.Size then
              Inc(result);
          finally
            srcFS.Free;
          end;
        end
        else
        begin
          { error }
        end;
      end;
    finally
      destFS.Free;
    end;
  end;
end;

上一篇:Delphi 二进制转换为文本下一篇:没有资料