Cutting files in packs

Hi,
I was wondering if anyone knew how to seperate files into segments for backup or something.

I mean, if I had a 3 MB file and wanted it to be split up into files of 1 MB each. If anyone has anycode to be able to do so (splitting up code and putting back together code) I would be extremely greatful.

Thanks,
SG
stephen_grahamAsked:
Who is Participating?
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.

umuligCommented:
inf:   file;
   outf:  file;
   size:  longint;
   outsize: longint;
   amt:     word;
   amtRead: word;

   assignfile (inf, 'input file');
   reset (inf, 1);
   size := fileSize (inf);
   repeat
      showMessage (enter floppy in "A")  { or "B" or allow them to specify }
      assignFile (outf, 'A:output file');
      rewrite (outf, 1);
      outsize := diskFree (1);  { or 2 if it's the "B" drive }
      while (outsize > 0) and (size > 0) do begin
         amt := sizeof(buf);
         if amt > outsize then amt := outsize;
         blockRead (inf, buf, amt, amtRead);
         blockWrite (outf, buf, amtRead);
         dec (outSize, amtRead);
         dec (size, amtRead);
      end;
      closeFile (outf);
   until size <= 0;
   closeFile (inf);


This is OTTOMH, syntax hasn't been checked, etc.  You may want to add other
code to let the user specify the "A" or "B" drive, and/or a naming scheme so
that if disks get out of order it's trapped.

Re-assembling the files is similar: open outf on the hard disk, ask the user
for the first floppy, blockRead/blockWrite from the floppy to the hard disk,
then ask the user for the next floppy, etc. until all floppies are read.

This might help

Cheers
Umulig
0
rwilson032697Commented:
Listening
0
stephen_grahamAuthor Commented:
Sorry,
Can't get your code to work Umulig. I have corrected most syntax but there are still problems. Could you rewrite the BlockWrite proc. for me if you have time (I have taken the buf var to be a string).

Thanks
SG
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

schutnikCommented:
buf should be array[0..size-1] of char.

pass buf[0] to the function BlockRead and BlockWrite.

Good luck
Kelly
0
kretzschmarCommented:
hi friends,

delphi provides a good streaming feature, therefore

a solution with streams:

procedure TForm1.Button1Click(Sender: TObject);
var
  i, WantedFragSize, RealToWrite : Integer;
  InStream, OutStream : TFileStream;
  S : String;
begin
  if Opendialog1.Execute then
  begin
    SaveDialog1.Title := 'Select Dir and input FilePrefix without Suffix';
    If SaveDialog1.Execute then
    begin
      WantedFragSize := StrtoInt(Edit1.Text);
      i := 0;
      InStream  := TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
      try
        while (InStream.Position < InStream.Size) do
        Begin
          s := IntToStr(I);
          while Length(s) < 3 do s := '0'+s;
          s := '.'+s;
          If InStream.Size - InStream.Position < WantedFragSize then
            RealToWrite := InStream.Size - InStream.Position
          else
            RealToWrite := WantedFragSize;
          OutStream  := TFileStream.Create(SaveDialog1.FileName+s,fmCreate);
          try
            OutStream.CopyFrom(InStream,RealToWrite);
            Inc(i);
          finally
            OutStream.Free;
          end;
        end;
      Finally
        InStream.Free;
      end;
    end;
  end;
end;

meikl
0
kretzschmarCommented:
hi stephen,

here the code to put the fragmented files together into one file as the original:

procedure TForm1.Button2Click(Sender: TObject);
var
  i : Integer;
  InStream, OutStream : TFileStream;
  SourcePath, FragName, S : String;
begin
  Opendialog1.Title := 'Select any Fragmented File';
  if Opendialog1.Execute then
  begin
    SourcePath := ExtractFilePath(Opendialog1.FileName);
    FragName := ExtractFileName(Opendialog1.FileName);
    SaveDialog1.Title := 'Select Dir and input the FileName with Suffix';
    If SaveDialog1.Execute then
    begin
      i := 0;
      s := IntToStr(I);
      while Length(s) < 3 do s := '0'+s;
      s := '.'+s;
      FragName := ChangeFileExt(FragName, s);
      If FileExists(SourcePath+FragName) then
      begin
        OutStream := TFileStream.Create(Savedialog1.FileName,fmCreate);
        try
          While FileExists(SourcePath+FragName) do
          Begin
            InStream  := TFileStream.Create(SourcePath+FragName,fmOpenRead);
            try
              OutStream.CopyFrom(InStream,0);
              Inc(i);
              s := IntToStr(I);
              while Length(s) < 3 do s := '0'+s;
              s := '.'+s;
              FragName := ChangeFileExt(FragName, s);
            finally
              InStream.Free;
            end;
          end;
        finally
          OutStream.Free;
        end;
      end;
    end;
  end;
end;

try it

meikl
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.

Start your 7-day free trial
stephen_grahamAuthor Commented:
Thanks alot everyone for helping but I found meikl's answer the best,

Thanks alot and here are your well awarded points!

SG
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.