How to copy multiple files into one #165
Question
Remember DOS? We can combine multiple ASCII files to one by using the
copy command like:
copy file1 + file2 + file3 file4
. That
makes file4
become the sum of file1
,
file2
and file3
. Does the
ShFileOperation API support this feature or is there any
other API support for this?
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;
Tip by Chris Willig
Original resource: | The Delphi Pool |
---|---|
Author: | Various |
Added: | 2010/06/02 |
Last updated: | 2010/06/02 |