Subject Re: How to Catch In Use By another error?
From Jan Hoelterling <jan@hoelterling.com>
Date Mon, 20 Nov 2017 07:31:55 -0700
Newsgroups dbase.getting-started

Hello Michael,

as you've found out, Exists() simply returns if the file handle has been
created. It does not tell you if the file in access by anything else.

I have used two methods to accomplish what you describe (assuming I
understood correctly):

1) check the file size before opening the file. In my experience, the
file size will be 0 bytes until the file is closed. This is because
Windows only updates the file size that is reported to other programs
once the file is closed. This may not hold true if the file exists
before it is written to by the other program (in other words, if the
other program only appends to the file).

2) This method will only work if you create a new file (in other words,
you are not simply appending to the file). Write to your file under a
*different* name. Then, when that is complete, try to delete the other
file (the existing file with the target name) and rename your file to
that name. The delete & rename will only succeed if the file is no
longer in use. You would need to wait for a specified time between
attempts, and for the sake of the user, you should allow for a graceful
exit if the other process is "hung" and never releases the file.

Your idea about using set reprocess to -1 will not work - that only
applies to access to DBF tables, not to files (at the operating system
level). Any attempt to open a file for write access will immediately
return success or fail.

HTH,

Jan