Board index » delphi » How to merge bin files like DOS(copy/b file1+file2 outfile )?

How to merge bin files like DOS(copy/b file1+file2 outfile )?

I need to merge 2 or more files like in DOS copy/b command where i can do:
copy/b  file1+file2+file3 outfile. I there a way  to do  that in Delphi
 

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


You can use AssignFile, BlockRead and BlockWrite.

--

Woody

Quote
Fabio <copern...@sti.com.br> wrote in message

news:s9e3e2bner268@corp.supernews.com...
I need to merge 2 or more files like in DOS copy/b command where i can do:
copy/b  file1+file2+file3 outfile. I there a way  to do  that in Delphi

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


Thanks Woody, it works!!
.Someone told me this and .....
use a stream or a file: you read form one file to a buffer and copy to
the other files, then read in the second file and continue copying to
the first one...

with a stream you can simply copy the entire stream at once, should be
easier.

Woody <woody....@ih2000.net> escreveu nas notcias de
mensagem:s9e47djcer...@corp.supernews.com...

Quote
> You can use AssignFile, BlockRead and BlockWrite.

> --

> Woody

> Fabio <copern...@sti.com.br> wrote in message
> news:s9e3e2bner268@corp.supernews.com...
> I need to merge 2 or more files like in DOS copy/b command where i can do:
> copy/b  file1+file2+file3 outfile. I there a way  to do  that in Delphi

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


Quote
Fabio wrote:
> Thanks Woody, it works!!
> .Someone told me this and .....
> use a stream or a file: you read form one file to a buffer and copy to
> the other files, then read in the second file and continue copying to
> the first one...

> with a stream you can simply copy the entire stream at once, should be
> easier.

Streams are slower because they do not use any caching. (on some
operations the factor is 50+ !!)

Bye, Udo

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


In article <p0rf9scsl50inss6g9du6hnffct8nj6...@4ax.com>, Udo Nesshoever

Quote
<newsgroup.re...@gmx.net> writes:
>Streams are slower because they do not use any caching. (on some
>operations the factor is 50+ !!)

Isn't that only for TFileStream & THandleStream. I think if you use
TMemoryStream it's pretty quick. I know I scanned (read 4 bytes at a time and
test, not just copied) a 1.25mB file in 1.7 secs. This was on a 100MHz Pentium
with 32Mb & Win95.

Also Ray Lischner has written some buffering stream server code (ie it can be
applied to any stream) in his "Secrets of Delphi 2"

Alan Lloyd
alangll...@aol.com

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


Use the Filestream Object.
What you do to merge the files is really quite simple if you think about it.
I'm not going to tell you exactly how to do it, but I'll point you in the
right direction.

To effectively do what you want to do you, you can't necessarily open the
file for text read.  What you really want is to read the whole file into a
character array or a memory buffer.

Aw, What the hell here's the code to do it.

I'm going to assume that you have the paths of the files you want to merge
in a string list or an array already;
I'm also assuming that you want to be able to Unmerge the Composite File as
well. If not you will easily see what you don't have to do.

We'll Need a few other variables to help us.

type PathString = string[64];    {You can make this string Longer to
accomodate larger path names if neccessary.}

Procedure MergeFiles(const ListOfFiles:tstringlist,Const OutputFile:string);

 Var
    FilePath:Pathstring;
    INSTREAM:TFilestream;
    OUTSTREAM:TFilestream;
    Filedate:TDateTime;
    FileSize:Integer;
    Buffer:Array[0..10000000] of char;// I know this sounds huge but, you
need
                                                            // this much
available memory to handle
                                                            // a worse case.
This is private variable
                                                            // so it won't
live past this procedure.

{Note: You need to set the maximum stack size using Program|Linker Options
to be large enough to handle a ten million character array or you will
generate a stack overflow error  See online help for instructions on how to
do this.  it's an easily adjustable project option.}

x:integer;
Begin
    OUTSTREAM:= tFilestream.create(OutputFile,fmCreate);
    for x:= 0 to ListOfFiles.count -1 do begin
            INSTREAM:= tFilestream.create(ListOfFiles.strings[x],
fmOpenRead); // Open Source File
            FilePath:= stringofchar(' ',64); // Initialize FilePath
            FilePath:= listofFiles.strings[x] + stringofchar(' ',64 -
length(ListOfFiles.strings[x]));
                    FileSize:= Instream.Size;
                    Filedate:= filedatetodatetime(instream.time);
                    Instream.Read(Buffer,Filesize);
                    {The Next 3 variables only need to be written if  you
intend to take this merged file back apart}

                        outstream.write(FilePath,64);  {You will use this
for the filename when you take the file apart}
                        outstream.write(FileSize,sizeof(filesize));  {This
is how many bytes of the merged file will be read back}
                        ouststream.write(FileDate,Sizeof(FileDate)); {This
is the date timestamp necessary if you need to Restore date}

            {Write The Contents of the source file.}
                    outstream.write(buffer,sizeof(Buffer));
                    instream.free;
end;
                    oustream.free;
end;
============================================================================
===
Presto!  The files are merged.  The extra variables you inserted into the
stream are necessary only if you must return the merged files to individual
files.
============================================================================
===
To Disassemble the File Simply Do This

Procedure UnMerge(const SourceFile:string);
var

 FilePath:Pathstring;
    INSTREAM:TFilestream;
    OUTSTREAM:TFilestream;
    Filedate:TDateTime;
    FileSize:Integer;
    Buffer:Array[0..10000000] of char;
    x:integer;

begin
        instream:= tfilestream.create(SourceFile,fmOpenRead);
        while instream.position < instream.size do begin {Since there is no
EOF marker you simply need to keep track of your
position in the File}
                        instream.read(FilePath,64);    {Path of Where File
Came From}
                        instream.read(FileSize,sizeof(Filesize));{How Many
Bytes to read back};
                        Instream.read(FileDate,SizeOf(Filedate));{dateTime
Stamp In case you need it}
                        Instream.read(buffer,filesize); {Extract Original
Data}
                        OUTSTREAM:=
tFileStream.create(trim(Filepath),fmCreate);
                        Outstream.write(buffer,FileSize); {Restore Data to
it's original Location}
                            { if you need to set the file's date and time do
it here}
                        Outstream.Free;  {Close The Restored File}
        end;
    instream.free;
End;
============================================================================
=====
There.  That's  one way you can merge files.

Hope it helps.

Stein Out.

Quote
Fabio <copern...@sti.com.br> wrote in message

news:s9e3e2bner268@corp.supernews.com...
Quote
> I need to merge 2 or more files like in DOS copy/b command where i can do:
> copy/b  file1+file2+file3 outfile. I there a way  to do  that in Delphi

Re:How to merge bin files like DOS(copy/b file1+file2 outfile )?


Thanks Paul, i  will try  to  implement that , Take a look at the code that
i'm  using  below .  (one bud here  sent  to me   and  it's  working
good!)..

function FileMerge(const Name1, Name2, Name3, NameDestination: String);
var
    F1, F2, F3, FDestination: File;
    Buffer: PByteArray;
    BufferSize: Integer;
begin
    AssignFile(FDestination, NameDestination);
    Rewrite(FDest, 1);

    AssignFile(F1, Name1);
    Reset(F1, 1);
    BufferSize := FileSize(F1);
    GetMem(Buffer, BufferSize);
    Try
        BlockRead(F1, Buffer^, BufferSize);
        BlockWrite(FDest, Buffer^, BufferSize);
    Finally
        FreeMem(Buffer);
        CloseFile(F1);
    end;

    AssignFile(F2, Name2);
    Reset(F2, 1);
    BufferSize := FileSize(F2);
    GetMem(Buffer, BufferSize);
    Try
        BlockRead(F2, Buffer^, BufferSize);
        BlockWrite(FDest, Buffer^, BufferSize);
    Finally
        FreeMem(Buffer);
        CloseFile(F2);
    end;

  AssignFile(F3, Name3);
    Reset(F3, 1);
    BufferSize := FileSize(F3);
    GetMem(Buffer, BufferSize);
    Try
        BlockRead(F3, Buffer^, BufferSize);
        BlockWrite(FDest, Buffer^, BufferSize);
    Finally
        FreeMem(Buffer);
        CloseFile(F3);
        CloseFile(FDest);
    end;

end;

Paul F. Steinbrueck <onlinest...@home.com> escreveu nas notcias de
mensagem:mGkI4.30125$Y4.841...@news1.rdc1.il.home.com...

Quote
> Use the Filestream Object.
> What you do to merge the files is really quite simple if you think about
it.
> I'm not going to tell you exactly how to do it, but I'll point you in the
> right direction.

> To effectively do what you want to do you, you can't necessarily open the
> file for text read.  What you really want is to read the whole file into a
> character array or a memory buffer.

> Aw, What the hell here's the code to do it.

> I'm going to assume that you have the paths of the files you want to merge
> in a string list or an array already;
> I'm also assuming that you want to be able to Unmerge the Composite File
as
> well. If not you will easily see what you don't have to do.

> We'll Need a few other variables to help us.

> type PathString = string[64];    {You can make this string Longer to
> accomodate larger path names if neccessary.}

> Procedure MergeFiles(const ListOfFiles:tstringlist,Const
OutputFile:string);

>  Var
>     FilePath:Pathstring;
>     INSTREAM:TFilestream;
>     OUTSTREAM:TFilestream;
>     Filedate:TDateTime;
>     FileSize:Integer;
>     Buffer:Array[0..10000000] of char;// I know this sounds huge but, you
> need
>                                                             // this much
> available memory to handle
>                                                             // a worse
case.
> This is private variable
>                                                             // so it won't
> live past this procedure.

> {Note: You need to set the maximum stack size using Program|Linker Options
> to be large enough to handle a ten million character array or you will
> generate a stack overflow error  See online help for instructions on how
to
> do this.  it's an easily adjustable project option.}

> x:integer;
> Begin
>     OUTSTREAM:= tFilestream.create(OutputFile,fmCreate);
>     for x:= 0 to ListOfFiles.count -1 do begin
>             INSTREAM:= tFilestream.create(ListOfFiles.strings[x],
> fmOpenRead); // Open Source File
>             FilePath:= stringofchar(' ',64); // Initialize FilePath
>             FilePath:= listofFiles.strings[x] + stringofchar(' ',64 -
> length(ListOfFiles.strings[x]));
>                     FileSize:= Instream.Size;
>                     Filedate:= filedatetodatetime(instream.time);
>                     Instream.Read(Buffer,Filesize);
>                     {The Next 3 variables only need to be written if  you
> intend to take this merged file back apart}

>                         outstream.write(FilePath,64);  {You will use this
> for the filename when you take the file apart}
>                         outstream.write(FileSize,sizeof(filesize));  {This
> is how many bytes of the merged file will be read back}
>                         ouststream.write(FileDate,Sizeof(FileDate)); {This
> is the date timestamp necessary if you need to Restore date}

>             {Write The Contents of the source file.}
>                     outstream.write(buffer,sizeof(Buffer));
>                     instream.free;
> end;
>                     oustream.free;
> end;

============================================================================
Quote
> ===
> Presto!  The files are merged.  The extra variables you inserted into the
> stream are necessary only if you must return the merged files to
individual
> files.

============================================================================

- Show quoted text -

Quote
> ===
> To Disassemble the File Simply Do This

> Procedure UnMerge(const SourceFile:string);
> var

>  FilePath:Pathstring;
>     INSTREAM:TFilestream;
>     OUTSTREAM:TFilestream;
>     Filedate:TDateTime;
>     FileSize:Integer;
>     Buffer:Array[0..10000000] of char;
>     x:integer;

> begin
>         instream:= tfilestream.create(SourceFile,fmOpenRead);
>         while instream.position < instream.size do begin {Since there is
no
> EOF marker you simply need to keep track of your
> position in the File}
>                         instream.read(FilePath,64);    {Path of Where File
> Came From}
>                         instream.read(FileSize,sizeof(Filesize));{How Many
> Bytes to read back};
>                         Instream.read(FileDate,SizeOf(Filedate));{dateTime
> Stamp In case you need it}
>                         Instream.read(buffer,filesize); {Extract Original
> Data}
>                         OUTSTREAM:=
> tFileStream.create(trim(Filepath),fmCreate);
>                         Outstream.write(buffer,FileSize); {Restore Data to
> it's original Location}
>                             { if you need to set the file's date and time
do
> it here}
>                         Outstream.Free;  {Close The Restored File}
>         end;
>     instream.free;
> End;

============================================================================

- Show quoted text -

Quote
> =====
> There.  That's  one way you can merge files.

> Hope it helps.

> Stein Out.

> Fabio <copern...@sti.com.br> wrote in message
> news:s9e3e2bner268@corp.supernews.com...
> > I need to merge 2 or more files like in DOS copy/b command where i can
do:
> > copy/b  file1+file2+file3 outfile. I there a way  to do  that in Delphi

Other Threads