[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 129
  • Last Modified:

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
0
stephen_graham
Asked:
stephen_graham
1 Solution
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now