Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Copying files in delphi using streams 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
Delphi All Versions
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

I'd like to be able to copy files in Delphi, but am having trouble figuring out how 
to do it. I've been using operating system level calls, but don't want to limited 
by them. Is there a way to do it in Delphi?


This is one of those topics that I've gotten asked about frequently enough that I 
decided it's time to write a short article on how to do it. It's funny that 
something as basic as this is not as visible as might be expected. It falls into a 
category that I call, "You gotta know what you're looking for..." Essentially, it 
means that the technique may not be hard to implement, it's just hard to find. In 
any case, once you know how to do it, it's not that difficult at all.

There are actually a number of ways to copy files. One way is to use untyped files 
along with BlockRead and BlockWrite. This also entails the use of an intermediary 
buffer. It works, but it can be a bit unwieldy, especially for novices. An easier 
way to accomplish file copying in Delphi is to use streams. As the term implies, a 
stream is sequential stream of data. When copying a file, you stream the file into 
a buffer, then stream buffer out to another file. Pretty simple in concept. Now in 
Delphi there are several types of streams which descend from the abstract base 
class TStream. I encourage you to look them up in the online help since they are 
beyond the scope of this discussion. But for our purposes, the descendant class 
that we're interested in is called TFileStream. This class allows applications to 
read from and write to files on disk. For simplicity's sake, I won't be going into 
the various intricacies of the class; again, encouraging you to study the online 
help. Or better yet, Ray Lischner's Book Secrets of Delphi 2 has a great discussion 
about streams as well (don't worry, the material applies to Delphi 3).

Quick and Dirty File Copying

The easiest method of copying a file with streams is called stream to stream 
copying. Essentially, this method involves creating a stream for the source file, 
and creating one for the destination file. Once that's done, it's a simple matter 
of copying the contents of the source stream to the destination stream. Listing 1 
below shows a procedure that encapsulates stream to stream copying:

1   {Quick and dirty stream copy}
3   procedure FileCopy(const FSrc, FDst: string);
4   var
5     sStream,
6       dStream: TFileStream;
7   begin
8     sStream := TFileStream.Create(FSrc, fmOpenRead);
9     try
10      dStream := TFileStream.Create(FDst, fmCreate);
11      try
12        {Forget about block reads and writes, just copy
13         the whole darn thing.}
14        dStream.CopyFrom(sStream, 0);
15      finally
16        dStream.Free;
17      end;
18    finally
19      sStream.Free;
20    end;
21  end;

Undoubtedly, you can get a lot more sophisticated with this. But for now, we'll leave it at this...

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


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC