Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy a file using a TFileStream Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
24-Aug-03
Category
Files Operation
Language
Delphi 2.x
Views
112
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler

How to copy a file using a TFileStream 

Answer:

1   { ... }
2   type
3     TFileCopyUpdateEvent = procedure(const SrcFile, DestFile: string;
4       CurrentPos, MaxSize: Integer) of object;
5   
6   function Min(Val1, Val2: Integer): Integer;
7   begin
8     Result := Val1;
9     if Val2 < Val1 then
10      Result := Val2;
11  end;
12  
13  {SrcFile and DestFile are the fully qualified filenames to the files to copy 
14  function}
15  
16  MyFileCopy(SrcFile, DestFile: TFilename; OnUpdate: TFileCopyUpdateEvent = nil):
17    Boolean;
18  const
19    StreamBuf = 4096;
20  var
21    Src, Dst: TFileStream;
22    BufCount: Integer;
23  begin
24    Src := nil;
25    Dst := nil; {prevents .Free problems on exception}
26    {allow everyone else any access}
27    Src := TFileStream.Create(SrcFile, fmOpenRead or fmShareDenyNone);
28    if FileExists(DestFile) then
29      {this could cause an error if a user has the file open}
30      Dst := TFileStream.Create(DestFile, fmOpenWrite or fmShareExclusive)
31    else
32      Dst := TFileStream.Create(DestFile, fmCreate or fmShareExclusive);
33    try
34      while Dst.Position < Dst.Size do
35      begin
36        BufCount := Min(StreamBuf, Dst.Size - Dst.Position);
37        Src.CopyFrom(Dst, BufCount);
38        if Assigned(OnUpdate) then {report progress every 4k}
39          OnUpdate(SrcFile, DestFile, Dst.Position, Dst.Size);
40      end;
41    finally
42      Src.Free;
43      Dst.Free;
44    end;
45  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC