Delphi waits for file copying to complete

I have a stream that I use ReadDirectoryChangesWfor notification when a file is added or deleted in a folder.

For each new image, I open the file and create a thumbnail of the image. It would seem, however, that I receive a notification before the file is completely copied to the destination folder, in which case I receive only a partial sketch. (Files are copied from remote locations to a central server, and the network may slow down during peak times.)

I check if the file is being used, but this does not work with image files.

HFileRes := CreateFile(pchar(Filename), GENERIC_READ or GENERIC_WRITE, 0, nil,   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ;
Result := (HFileRes = INVALID_HANDLE_VALUE);
if (not Result) then
  CloseHandle(HFileRes) ;

My question is this: is there a way to detect when the file is fully copied, or am I just waiting for the file size or the time of the last modification to change from the moment of the last check?

Regards, Peter

+5
source share
1 answer

To verify that the file check is complete, first check to see if you can get exclusive access.

  FileHandle := FileOpen(FileName, fmOpenRead or fmShareExclusive);
  if FileHandle > 0 then
    {valid file handle}
+1
source

All Articles