Solved

Delphi : How to copy from c: to a:

Posted on 1997-05-07
3
214 Views
Last Modified: 2010-04-06
How do I copy some files from c: to a:??
Help me??
0
Comment
Question by:Ikke
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now