0

I have code that calls RenameFile().

The Result passed back is sometimes False; this appears to happen when the (.pdf) file is in use.

I have seen what appears to be a 'standard' message loop in everyday programs where, if an operation can not be executed against a file that is in use, the message prompts the user to close the file and try again (or Cancel).

Is there a function in Delphi that does this, or does anyone have a coding suggestion that mimics this behavior?

I am working in Delphi-Tokyo. Thanks!

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36
Andrew W
  • 67
  • 1
  • 4
  • Add the code that you are using now. What you are asking is simple and it is referred to as Error handling use the classic `try .. except` block and handle the IO exception when the file is in use. – Nasreddine Galfout Apr 16 '20 at 05:08
  • Perhaps you are thinking about a modal dialog with e.g. `Retry` and `Cancel` buttons. You show the dialog and check return value with `if MyDialog.ShowModal = mrOK then ...`. In the dialog you assign `ModalResult` according to users actions. See help for `ShowModal` and related topics. – Tom Brunberg Apr 16 '20 at 08:19
  • I have a try..except block around the call to RenameFile; no exception is thrown; the function simply returns False, and the flow of execution continues. – Andrew W Apr 16 '20 at 13:22
  • Yes, I envision a while loop that involves a modal dialog. But that doesn't resolve the dilemma of knowing for what specific reason, the call to RenameFile returned False. I thought Delphi might have another function, or there is some other way to go about this, that I can evaluate the actual reasons for the RenameFile failing, so I can either communicate to the user that there is something they can do to make another call to RenameFile successful, or I need to do some other coding because the reason for it failing is beyond the user's control. – Andrew W Apr 16 '20 at 13:36
  • 2
    You can check the API result via `GetLastError` immediately after calling `Rename()` and translating the error code to a message for the user. – Tom Brunberg Apr 16 '20 at 17:13
  • Thank you @TomBrunberg , I can work with that. :) – Andrew W Apr 16 '20 at 20:25

0 Answers0