Solved

Delphi : How to copy from c: to a:

Posted on 1997-05-07
3
209 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
System restore point 4 79
Delphi XE10 Round Image 2 56
Convert GUI app into console app for Win32 Env 5 60
LAN or WAN ? 11 66
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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

12 Experts available now in Live!

Get 1:1 Help Now