DeleteFile does not work

It seems that somewhere in my code the file I am working with is somehow holding on because I cannot delete it. My CopyFile program seems to work, but after I copied it, I cannot 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 breakpoint on the line if not DeleteFile
this always raises an exception. Matching InPath and InFileName Used in CopyFile Procedure

In any case, I always get an error, trying to delete the file. Does this have anything to do with file size? I only copy and delete if file size <= 0

+3
source share
2 answers

If you do not know why you did not delete the file, why not let the operating system tell you? Replace this code:

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

with

RaiseLastOSError

, , , , :

  • .
  • , .
  • , , .
  • .

: - , RUN IDE, Windows .

+11

, delete, MoveFile() MoveFileEx()?
, Windows API MoveFile() / .

0

All Articles