Page 1 of 1

How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 3:43 am
by Eugene Lutsenko
Whether the file you need to know before removing or replacing, to issue an appropriate message to the user before execution error occurred.

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 6:03 am
by Auge_Ohr
Eugene Lutsenko wrote:Whether the file you need to know before removing or replacing, to issue an appropriate message to the user before execution error occurred.
using "SHFileOperationA" API with "Copy" / "Rename" you can get a Warning like in Explorer.

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 7:19 am
by rdonnay

Code: Select all

IF !FExists('C:\test\junk.dbf')
  DCMSGBOX 'File does not Exist!'
  RETURN .f.
ENDIF

IF FExists('C:\test\junk.dbf')
  DCMSGBOX 'File Exists!.  Overwrite?' YESNO TO lStatus
  IF lStatus
    // Overwrite file
  ENDIF
ENDIF
RETURN lStatus 

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 7:26 am
by Eugene Lutsenko
Thank you!
And how to determine the file is opened or closed, that is, whether it is used? My question is not about DBF file, and about XLS.

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 7:37 am
by Tom
MS Office creates a hidden file with a prefix "~$" when a file (xls, doc) is opened. So, you can check if "~$myfile.xlsx" exists if you're trying to delete "myfile.xlsx". This indicates that the file may be in use. Sometimes, those temporary files are not deleted properly, so the temporary file (containing the windows-username of the person who opened it) is still there. The best indicator that a file is in use is: Try to delete it. If this fails, it is in use. ;)

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 8:10 am
by rdonnay
And how to determine the file is opened or closed, that is, whether it is used? My question is not about DBF file, and about XLS.
Give this a try:

Code: Select all

FUNCTION IsFileOpened( cFileName )

#include "fileio.ch"
LOCAL lStatus := .t. 
LOCAL nHandle := FOpen( cFileName, FO_READWRITE+FO_DENYWRITE) )

IF nHandle <= 0
  DCMSGBOX 'File is in use.  Try again later!'
  lStatus := .f.
ELSE
  FClose(nHandle)
ENDIF

RETURN lStatus

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 9:09 am
by Eugene Lutsenko
Thank you so much! Try as would be the time. I think everything will turn out. How to tell if a file in the folder, or it is not of course I know. Simply because of the lack of English and the use of an automated translator can not articulate the question. Once again, I apologize and thank you so much.

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 10:51 am
by Eugene Lutsenko
Tom wrote:MS Office creates a hidden file with a prefix "~$" when a file (xls, doc) is opened. So, you can check if "~$myfile.xlsx" exists if you're trying to delete "myfile.xlsx". This indicates that the file may be in use. Sometimes, those temporary files are not deleted properly, so the temporary file (containing the windows-username of the person who opened it) is still there. The best indicator that a file is in use is: Try to delete it. If this fails, it is in use. ;)
Simple and original thought. Strange that she did not come to my head ...

Re: How to know whether the file is not trying to open it?

Posted: Mon Oct 27, 2014 11:26 am
by Eugene Lutsenko
rdonnay wrote:
And how to determine the file is opened or closed, that is, whether it is used? My question is not about DBF file, and about XLS.
Give this a try:

Code: Select all

FUNCTION IsFileOpened( cFileName )

#include "fileio.ch"
LOCAL lStatus := .t. 
LOCAL nHandle := FOpen( cFileName, FO_READWRITE+FO_DENYWRITE) )

IF nHandle <= 0
  DCMSGBOX 'File is in use.  Try again later!'
  lStatus := .f.
ELSE
  FClose(nHandle)
ENDIF

RETURN lStatus
Roger! Thank you so much! Everything turned out great! I know the file is open or not without trying to open it (and other operations without check temporary files) and it is obtained for all types of files and programs using them. This is exactly what I needed.