Solved

Delphi : How to copy from c: to a:

Posted on 1997-05-07
3
228 Views
Last Modified: 2010-04-06
How do I copy some files from c: to a:??
Help me??
0
Comment
Question by:Ikke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
gysbert1 earned 50 total points
ID: 1335974
Here is some code from the delphi2/demos/doc/filemanex directory

const
  SInvalidDest = 'Destination %s does not exist';
  SFCantMove = 'Cannot move file %s';

procedure CopyFile(const FileName, DestName: TFileName);
var
  CopyBuffer: Pointer; { buffer for copying }
  TimeStamp, BytesCopied: Longint;
  Source, Dest: Integer; { handles }
  Destination: TFileName; { holder for expanded destination name }
const
  ChunkSize: Longint = 8192; { copy in 8K chunks }
begin
  Destination := ExpandFileName(DestName); { expand the destination path }
  if HasAttr(Destination, faDirectory) then { if destination is a directory... }
    Destination := Destination + '\' + ExtractFileName(FileName); { ...clone file name }
  TimeStamp := FileAge(FileName); { get source's time stamp }
  GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  try
    Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
    if Source < 0 then raise EFOpenError.Create(FmtLoadStr(SFOpenError, [FileName]));
    try
      Dest := FileCreate(Destination); { create output file; overwrite existing }
      if Dest < 0 then raise EFCreateError.Create(FmtLoadStr(SFCreateError, [Destination]));
      try
        repeat
          BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }
          if BytesCopied > 0 then { if we read anything... }
            FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
        until BytesCopied < ChunkSize; { until we run out of chunks }
      finally
        FileClose(Dest); { close the destination file }
      end;
    finally
      FileClose(Source); { close the source file }
    end;
  finally
    FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  end;
end;


If you need the entire file (DPR included and MOVE functions, etc drop me a comment and I can Mail it to you if you do not have it.

Since the above is only a part of the example it may not work on it's own but it gives you the way to do it ...
0
 
LVL 1

Expert Comment

by:sleach
ID: 1335975
This is much shorter but you have much more control over the process if you do it the Delphi Example way. You could for example add a progress bar to your app when you use the example code which is impossible with the API call. This is essential if you are writing a comercial app that is going to copy large files.

I do not know what error handling (if any) the API provides. If you are writing a commercial app rather use the proposed code. If this is for private use and you need a quick solution go API.
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335976
For me, the best way to copy a file from C: to A: is to use
the PROCEDURE BlockRead and Blockwrite from the PASCAL.
Exeption error from DELPHI (no disk, disk not formated...) are
not a problem. Simply click the OK button when the new disk is
inserted in the drive.
press CTRL+F1 in the word BlockRead in the editor of DELPHI and
you have a simple example to put in a library.

They are also many library for this operation on INTERNET
Contact me for the best of this.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 82
How to build JSON File in Delphi 6 3 94
DBGrid or StringGrid ? 6 135
Tidtcpserver listening on multiports? 1 54
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question