Link to home
Start Free TrialLog in
Avatar of stephen_graham
stephen_graham

asked on

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
Avatar of umulig
umulig

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
Listening
Avatar of stephen_graham

ASKER

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
buf should be array[0..size-1] of char.

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

Good luck
Kelly
Avatar of kretzschmar
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
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot everyone for helping but I found meikl's answer the best,

Thanks alot and here are your well awarded points!

SG