Solved

Why does CopyFile never fail?

Posted on 2008-10-21
4
610 Views
Last Modified: 2010-08-05
I am using CopyFile to make a backup of an important file. It seems that CopyFile always returns true, even if the path is not accessible, or the disk or file are read-only. What system do you recommend to make sure that the file has been copied successfully?
ERROR := Not CopyFile(PChar(ConfigFilename),PChar(ConfigBackup),False);

Open in new window

0
Comment
Question by:momsoft2
[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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:ThievingSix
ID: 22769131
Here's a dirty way to get it done. There are better ways to do it though.
function CopyFile(lpExistingFileName, lpNewFileName: PChar; bFailIfExists: LongBool): LongBool;
var
  FileStreamExisting,
  FileStreamNew : TFileStream;
  Buffer : PChar;
begin
  Result := False;
  If (bFailIfExists) And (FileExists(lpNewFileName)) Then Exit;
  FileStreamExisting := TFileStream.Create(lpExistingFileName,fmOpenRead);
  FileStreamNew := TFileStream.Create(lpNewFileName,fmCreate);
  Try
    If FileStreamNew.CopyFrom(FileStreamExisting,0) = FileStreamExisting.SIze Then
      begin
      Result := True;
    end;
  Finally
    FileStreamExisting.Free;
    FileStreamNew.Free;
  end;
end;

Open in new window

0
 

Author Comment

by:momsoft2
ID: 22769474
Thank you very much. This is definitely a step in the right direction.

Your function does indeed fail if the destination is invalid, but then, the original file stays locked and I get errors if I try to access it again.
0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 125 total points
ID: 22769819
This should always free it with or without exceptions.
function CopyFile(lpExistingFileName, lpNewFileName: PChar; bFailIfExists: LongBool): LongBool;
var
  FileStreamExisting,
  FileStreamNew : TFileStream;
  Buffer : PChar;
begin
  Result := False;
  If (bFailIfExists) And (FileExists(lpNewFileName)) Then Exit;
  FileStreamExisting := TFileStream.Create(lpExistingFileName,fmOpenRead);
  Try
    FileStreamNew := TFileStream.Create(lpNewFileName,fmCreate);
    Try
      If FileStreamNew.CopyFrom(FileStreamExisting,0) = FileStreamExisting.SIze Then
        begin
        Result := True;
      end;
    Finally
      FileStreamNew.Free;
    end;
  Finally
    FileStreamExisting.Free;
  end;
end;

Open in new window

0
 

Author Closing Comment

by:momsoft2
ID: 31508387
Thank you very much for your help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 132
DBGrid or StringGrid ? 6 133
Delphi android app hide keyboard 3 78
how can i delete one control based on its tagstring ? 6 56
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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