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

This forum is for eXpress++ general support.
Post Reply
Message
Author
User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

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

#1 Post 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.

User avatar
Auge_Ohr
Posts: 1428
Joined: Wed Feb 24, 2010 3:44 pm

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

#2 Post 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.
greetings by OHR
Jimmy

User avatar
rdonnay
Site Admin
Posts: 4813
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

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

#3 Post 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 
The eXpress train is coming - and it has more cars.

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

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

#4 Post 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.

User avatar
Tom
Posts: 1234
Joined: Thu Jan 28, 2010 12:59 am
Location: Berlin, Germany

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

#5 Post 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. ;)
Best regards,
Tom

"Did I offend you?"
"No."
"Okay, give me a second chance."

User avatar
rdonnay
Site Admin
Posts: 4813
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

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

#6 Post 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
The eXpress train is coming - and it has more cars.

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

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

#7 Post 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.

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

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

#8 Post 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 ...

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

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

#9 Post 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.

Post Reply