4
votes

It seems that somewhere in my code the file i am working with has a hold on it somehow, cause i can't seem to delete it. My CopyFile routine seems to work, but after i am done copying it, i can't seem to delete it.

   FSize:=  GetFileSizeExt(InPath + InFileName);
   if FSize <= 0 then
   begin
    //archive file
    if  AfterAction = 'MOVE' then
    begin
     tmpExt:= ExtractFileExt(InFileName);
     if CopyFile(PChar(InPath + InFileName), PChar(MovePath + '\' + ChangeFileExt(InFileName,'') + '_' + FormatDateTime('mmddyyyy-hhmmss', Now) + tmpExt), True) then
     begin
      if not DeleteFile(pchar(InPath + InFileName)) then
      begin
       ExitCode:= 8;
       raise ECustomException.Create('Invalid After Action. Error Deleting File!');
      end;
     end //if CopyFile
     else //if not DeleteFile
     begin
      ExitCode:= 16;
      raise ECustomException.Create('File Copy Error!');
     end; //else
    end; //if  AfterAction = 'MOVE' then
     ExitCode:= 17;
     raise ECustomException.Create('Error Getting file size OR file size less than or equal to zero!');
   end; //if filesize =0

when I set a break point on the line
if not DeleteFile
it always ends up raising the exception. The InPath & InFileName match that used in the
CopyFile routine

Anyways, i always get the error try to delete the file. Does this have anything to do with File Size? I only copy and delete if file size <= 0

2
It might be possible, that the file you are copying is still open because of code executed before the copying procedure. (i.e. in code you didn't post) - Daniel Rikowski
DeleteFile is working. One of its requirements is not to delete files which are not allowed to be deleted. It is succeeding. Find out what is stopping the file from being deleted. If another process has it locked then Process Explorer will tell you. - David Heffernan
I tested the file with data in it and the code works fine. If i remove the data from the file, so file size = 0, then it doesn't work. I get an exception. So, it has to do with filesize. - IElite
Why don't you just call MoveFile? It offers certain advantages that CopyFile doesn't have (e.g., link tracking, security-descriptor maintenance, speed). - Rob Kennedy
I've had a similar issue. I've downloaded 6 CSV files from the same website. Have not tried to open any of them so they can't be in use, and than ran a routine to delete each file one at a time. The 1st and last file in the list will not delete but the 4 in between delete without issue. I'm using a function I found on StackOverflow. function DeleteFileRB(FileName:string): boolean; - Jerry Mallett

2 Answers

13
votes

If you don't know why you fail deleting the file, why not let the operating system tell you? Replace this code:

raise ECustomException.Create('Invalid After Action. Error Deleting File!');

with

RaiseLastOSError

I don't think the problem's related to the code you're showing, so here's a list of things to check:

  • The file is not read-only.
  • The file is not on read-only media.
  • The user running the application has the right to delete the file.
  • The file is not in use.

An other thing I'd do: When the exception about not being able to delete the file is raised, before I hit RUN in the IDE, I'd go to Windows Explorer and try deleting the file myself.

0
votes

Why don't you use, instead of copy then delete, the MoveFile() or MoveFileEx() function ?
It do the same work but the Windows API MoveFile() function can't rename/move files across drives.