Solved

Why does CopyFile never fail?

Posted on 2008-10-21
4
601 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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