# Concatenating files programmatically

I want to append or concatenate together some files programmatically. Is there a way I can call the DOS 'COPY' command from Delphi, so I can issue the command 'COPY file1.txt+file2.txt+file3.txt result.txt? Or is there some Delphi command that will do it for me?

Thanks,
Shawn Halfpenny

P.S: new e-mail address : drumme59@sprint.ca
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
procedure TForm1.Button1Click(Sender: TObject);
var
lOut : tFileStream;
lIn  : tFileStream;
begin
lOut := tFileStream.Create( 'c:\temp\out' , fmCreate );

lIn  := tFileStream.Create( 'c:\temp\in.1' , fmOpenRead );
lOut.CopyFrom( lIn , lIn.Size );
lIn.Free;

lIn := tFileStream.Create( 'c:\temp\in.2' , fmOpenRead );
lOut.CopyFrom( lIn , lIn.Size );
lIn.Free;

{ etc.! }

lOut.Free;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Or just trap exceptions from the filestream objects ....

0
Commented:
Yes, but if you need progress bar then what ?
0
Commented:
Then copy not all in one step, but this is the same as BlockRead. This is only other idea.
0
Commented:
adrewjb - Sorry for my next comment, but what about sharing ? I don;t know if it is possible with stream. Do you know about this.
0
Commented:
The second parameter of the Create call can lock the file, I think.

0
Commented:
Yes You have right. The direct file functions is better only when more than one user often updates the file. Better means so You can write program easly. Stupid example can be file for conversation within some users (I say so this is stupid !).
Thank you. I just will be using whenever it is possible FileStream.
0
Commented:
function ConcatFiles(inFilename1,inFilename2,outFilename : string) : boolean;
const BUFFERSIZE                = $FFFF; var inFile1, inFile2, outFile : file; buffer : pointer; readbytes : integer; begin {$I-}

{Open all the files}
AssignFile(inFile1,inFilename1); Reset(inFile1,1);
if IOResult <> 0 then begin ConcatFiles := false; exit end;
AssignFile(inFile2,inFilename2); Reset(inFile2,1);
if IOResult <> 0 then begin ConcatFiles := false; exit end;
AssignFile(outFile,outFilename); ReWrite(outFile,1);
if IOResult <> 0 then begin ConcatFiles := false; exit end;

{allocate memory for the buffer}
getMem(buffer,BUFFERSIZE);

{copy file 1}
while not EOF(inFile1) do begin
end;

{copy file 2}
while not EOF(inFile2) do begin
end;

{free the buffer memory}
freeMem(buffer,BUFFERSIZE);

{close all the files}
CloseFile(inFile1); CloseFile(inFile2); CloseFile(outFile);

{\$I+}
ConcatFiles := true;
end;

0
Commented:
hi mes ! This differs from my comment only in buffer. Your buffer is better of course, but answer from adrewjb is the best.
0
Commented:
Hi, I'd like to re-submit my grade for AndrewJB's answer to this. He really did give an excellent answer - it works beautifully - so I'd like to give him an A.

Cheers
Shawn Halfpenny
drumme59@sprint.ca
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.