Solved

Concatenating  files programmatically

Posted on 1997-10-09
10
259 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
  • 6
  • 3
10 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 50 total points
Comment Utility
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
Comment Utility
Or just trap exceptions from the filestream objects ....

0
 
LVL 12

Expert Comment

by:andrewjb
Comment Utility
Yes, but if you need progress bar then what ?
0
 
LVL 3

Expert Comment

by:mirek071497
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

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

0
 
LVL 12

Expert Comment

by:andrewjb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now