Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is the file open ?????

Status
Not open for further replies.

hawkieboy

Programmer
Apr 30, 2002
49
GB
Hey all

Just a short one. is there away i can test to see if a specific file is open.

Many Thanks

Nick

just in case it helps. the file i need to know about is a excel file.

your help and advice is appreciated
 
You could try and Open it with FOPEN(). Since this always opens files "exclusive", if you can't open it, then it must be in use by someone else!

Rick
 
Hi Rick

I don't want to open the file as The file gets open by the user and I need to know if the user has accidently left the file open.

sorry if i seem a bit stupid. i dont really understand fopen()

Thanks for the reply

Nick

your help and advice is appreciated
 
hawkieboy,

Try this:
++++++++++++
* IsFileOpen.prg

LPARAMETERS tcFileName

#DEFINE TRUE .T.
#DEFINE FALSE .F.
#DEFINE MB_ICONSTOP 16 && Critical message
#DEFINE F_READWRITE_UNBUFF 12
#DEFINE F_OPEN_FAILURE -1

LOCAL llRtnVal as Boolean
LOCAL lnHandle as Number

llRtnVal = TRUE && assume file is closed

lnHandle = FOPEN(tcFileName,F_READWRITE_UNBUFF)
IF lnHandle = F_OPEN_FAILURE
llRtnVal = FALSE && file is open
MESSAGEBOX(ALLTRIM(tcFileName)+' file open by someone else',MB_ICONSTOP))
ENDIF

=FCLOSE(lnHandle) && close the file

RETURN llRtnVal
++++++++++
Harry
 
As the above comments indicate, the only quick way to test to see if a file is open is to attempt to open it exclusively. They are the best and simplest way to determine if a table is open.

Otherwise, you'll have to walk over to the server and look through the server's list of open files, but you can't really trust that list 100%. A good example of that is when a workstation crashes, the files it had open probably are not open any longer, but those links could persist for a while at the server and erroneously indicate it was still open.
 
I came across a VB script that I converted to VFP that will create a cursor of open files, and who has them open on the server.

Create cursor isopen (f_user c(10), f_path c(30) )
Store 1 to ColumnNumber,RowNumber
LcOfile='.DBF'

** Change MyServer to fit your needs.
fso = GetObject("WinNT://MyServer/LanmanServer")
For Each resource In fso.resources
if type('resource.user')!='C'
exit
endif
If resource.User!= "" or Right(resource.User,1)!= "$"
If occurs(lcOfile, upper(resource.path) )>0 or lcOfile==''
m.f_user = resource.user
m.f_path = resource.Path
insert into isopen from memvar
Endif
Endif
Endfor

select isopen
if reccount()>0
browse
else
messagebox('No Open Files')
endif

**************************************************

There is also a simple utility that I found, that will allow you to close file on a server- haven't done anything with it yet.


Good luck, I hope this helps.

Hal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top