Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Concatenating  files programmatically

Posted on 1997-10-09
10
Medium Priority
?
267 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 1347132
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
 
LVL 3

Expert Comment

by:mirek071497
ID: 1347133
Or just trap exceptions from the filestream objects ....

0
 
LVL 12

Expert Comment

by:andrewjb
ID: 1347134
Yes, but if you need progress bar then what ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:mirek071497
ID: 1347135
Then copy not all in one step, but this is the same as BlockRead. This is only other idea.
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1347136
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
 
LVL 3

Expert Comment

by:mirek071497
ID: 1347137
The second parameter of the Create call can lock the file, I think.

0
 
LVL 12

Expert Comment

by:andrewjb
ID: 1347138
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
 
LVL 3

Expert Comment

by:mirek071497
ID: 1347139
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
    BlockRead(inFile1,buffer^,BUFFERSIZE,readbytes);
    BlockWrite(outFile,buffer^,readbytes);
  end;

  {copy file 2}
  while not EOF(inFile2) do begin
    BlockRead(inFile2,buffer^,BUFFERSIZE,readbytes);
    BlockWrite(outFile,buffer^,readbytes);
  end;

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

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

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


0
 

Expert Comment

by:mes
ID: 1347140
hi mes ! This differs from my comment only in buffer. Your buffer is better of course, but answer from adrewjb is the best.
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1347141
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question