[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Delphi : How to copy from c: to a:

How do I copy some files from c: to a:??
Help me??
0
Ikke
Asked:
Ikke
  • 2
1 Solution
 
gysbert1Commented:
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
 
sleachCommented:
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
 
gysbert1Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now