home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0094_Copying files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  5.4 KB  |  207 lines

  1.  
  2. {This way uses a File stream.}
  3. Procedure FileCopy( Const sourcefilename, targetfilename: String );
  4. Var
  5.   S, T: TFileStream;
  6. Begin
  7.   S := TFileStream.Create( sourcefilename, fmOpenRead );
  8.   try
  9.     T := TFileStream.Create( targetfilename, fmOpenWrite or fmCreate );
  10.     try
  11.       T.CopyFrom(S, S.Size ) ;
  12.     finally
  13.       T.Free;
  14.     end;
  15.   finally
  16.     S.Free;
  17.   end;
  18. End;
  19.  
  20.  
  21. {Here is one that uses a TMemoryStream:}
  22. procedure FileCopy(const FromFile, ToFile: string);
  23. begin
  24.   with TMemoryStream.Create do
  25.   try
  26.     LoadFromFile(FromFile);
  27.  
  28.     SaveToFile(ToFile);
  29.   finally
  30.     Free;
  31.   end;
  32. end;
  33.  
  34.  
  35. {This way uses memory blocks for read/write.}
  36. procedure FileCopy(const FromFile, ToFile: string);
  37.  var
  38.   FromF, ToF: file;
  39.   NumRead, NumWritten: Word;
  40.   Buf: array[1..2048] of Char;
  41. begin
  42.   AssignFile(FromF, FromFile);
  43.   Reset(FromF, 1);        { Record size = 1 }
  44.   AssignFile(ToF, ToFile);    { Open output file }
  45.   Rewrite(ToF, 1);        { Record size = 1 }
  46.   repeat
  47.     BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
  48.     BlockWrite(ToF, Buf, NumRead, NumWritten);
  49.   until (NumRead = 0) or (NumWritten <> NumRead);
  50.  
  51.   System.CloseFile(FromF);
  52.   System.CloseFile(ToF);
  53. end;
  54.  
  55. {This one uses LZCopy, which USES LZExpand.}
  56. procedure CopyFile(FromFileName, ToFileName: string);
  57. var
  58.   FromFile, ToFile: File;
  59. begin
  60.   AssignFile(FromFile, FromFileName); { Assign FromFile to FromFileName }
  61.   AssignFile(ToFile, ToFileName);     { Assign ToFile to ToFileName }
  62.   Reset(FromFile);                    { Open file for input }
  63.   try
  64.     Rewrite(ToFile);                  { Create file for output }
  65.     try
  66.       { copy the file an if a negative value is returned raise an exception }
  67.  
  68.       if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0 then
  69.         raise Exception.Create('Error using LZCopy')
  70.     finally
  71.       CloseFile(ToFile);  { Close ToFile }
  72.     end;
  73.   finally
  74.     CloseFile(FromFile);  { Close FromFile }
  75.   end;
  76. end;
  77.  
  78.  
  79. This one is from Dr. Bob (Swart).  The point of this one is that it contains a callback function that gives you the ability to callback.  This can be used for progress bars and the like.  Groetjes, Dr. Bob!
  80.  
  81.  
  82.  {$A+,B-,D-,F-,G+,I+,K+,L-,N+,P+,Q-,R-,S+,T+,V-,W-,X+,Y-}
  83.  unit FileCopy;
  84.  
  85.  (*
  86.    FILECOPY 1.5 (Public Domain)
  87.    Borland Delphi 1.0
  88.    Copr. (c) 1995-08-27 Robert E. Swart (100434.2072@compuserve.com)
  89.                         P.O. box 799
  90.                         5702 NP  Helmond
  91.                         The Netherlands
  92.    -----------------------------------------------------------------
  93.    This unit implements a FastFileCopy procedure that is usable from
  94.    Borland Pascal (real mode, DPMI or Windows) and Borland Delphi. A
  95.    callback routine (or nil) can be given as extra argument.
  96.  
  97.    Example of usage:
  98.  
  99.    {$IFDEF WINDOWS}
  100.     uses FileCopy, WinCrt;
  101.    {$ELSE}
  102.     uses FileCopy, Crt;
  103.  
  104.    {$ENDIF}
  105.  
  106.       procedure CallBack(Position, Size: LongInt); far;
  107.       var i: Integer;
  108.       begin
  109.         { do you stuff here... }
  110.         GotoXY(1,1);
  111.         for i:=1 to (80 * Position) div Size do write('X')
  112.       end {CallBack};
  113.  
  114.     begin
  115.       FastFileCopy('C:\AUTOEXEC.BAT', 'C:\AUTOEXEC.BAK', nil);
  116.       FastFileCopy('C:\CONFIG.SYS', 'C:\CONFIG.BAK', CallBack)
  117.     end.
  118.  *)
  119.  interface
  120.  
  121.  Type
  122.    TCallBack = procedure (Position, Size: LongInt); { export; }
  123.  
  124.    procedure FastFileCopy(Const InFileName, OutFileName: String;
  125.                           CallBack: TCallBack);
  126.  
  127.  
  128.  implementation
  129.  {$IFDEF VER80}
  130.  uses SysUtils;
  131.  {$ELSE}
  132.    {$IFDEF WINDOWS}
  133.    uses WinDos;
  134.    {$ELSE}
  135.    uses Dos;
  136.    {$ENDIF}
  137.  {$ENDIF}
  138.  
  139.    procedure FastFileCopy(Const InFileName, OutFileName: String;
  140.                           CallBack: TCallBack);
  141.    Const BufSize = 8*4096; { 32Kbytes gives me the best results }
  142.    Type
  143.      PBuffer = ^TBuffer;
  144.      TBuffer = Array[1..BufSize] of Byte;
  145.    var Size: Word;
  146.        Buffer: PBuffer;
  147.        infile,outfile: File;
  148.        SizeDone,SizeFile,TimeDateFile: LongInt;
  149.    begin
  150.      if (InFileName <> OutFileName) then
  151.      begin
  152.        Buffer := nil;
  153.        Assign(infile,InFileName);
  154.  
  155.        System.Reset(infile,1);
  156.        {$IFDEF VER80}
  157.        try
  158.        {$ELSE}
  159.        begin
  160.        {$ENDIF}
  161.          SizeFile := FileSize(infile);
  162.          Assign(outfile,OutFileName);
  163.          System.Rewrite(outfile,1);
  164.          {$IFDEF VER80}
  165.          try
  166.          {$ELSE}
  167.          begin
  168.          {$ENDIF}
  169.            SizeDone := 0;
  170.            New(Buffer);
  171.            repeat
  172.              BlockRead(infile,Buffer^,BufSize,Size);
  173.              Inc(SizeDone,Size);
  174.              if (@CallBack <> nil) then
  175.                CallBack(SizeDone,SizeFile);
  176.  
  177.              BlockWrite(outfile,Buffer^,Size)
  178.            until Size < BufSize;
  179.            {$IFDEF VER80}
  180.            FileSetDate(TFileRec(outfile).Handle,
  181.              FileGetDate(TFileRec(infile).Handle));
  182.            {$ELSE}
  183.            GetFTime(infile, TimeDateFile);
  184.            SetFTime(outfile, TimeDateFile);
  185.            {$ENDIF}
  186.          {$IFDEF VER80}
  187.          finally
  188.          {$ENDIF}
  189.            if Buffer <> nil then Dispose(Buffer);
  190.            System.close(outfile)
  191.          end;
  192.        {$IFDEF VER80}
  193.        finally
  194.        {$ENDIF}
  195.  
  196.          System.close(infile)
  197.        end
  198.      end
  199.      {$IFDEF VER80}
  200.      else
  201.        Raise EInOutError.Create('File cannot be copied onto itself')
  202.      {$ENDIF}
  203.    end {FastFileCopy};
  204.  end.
  205.  
  206.  
  207.