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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if FILE is open 2

Status
Not open for further replies.

mikeisvfp

Programmer
Mar 5, 2011
91
CA
Hello Experts

How would I check to see if a FILE is already open?
 
What kind of FILE? Open by who?

Do you need to know if your currently running application has something (DOC, DBF, TXT?) open? Do you need to know if someone else has it open?

 
If you want to know if a file is available to be opened by your application exclusively (for example) just use something like this:

Code:
m.FILEHANDLE = FCREATE("c:\myfolder\myfile.txt")
IF m.FILEHANDLE < 0  && Check for error opening file
	MESSAGEBOX("Unable to Access ",0,"Error")
	QUIT
ENDIF

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Bear in mind the above approach is going to destroy the contents of c:\myfolder\myfile.txt if it is successful!

You might prefer to use fopen() if you want to preserve the file:

Code:
m.FILEHANDLE = FOPEN("c:\myfolder\myfile.txt",2)
IF m.FILEHANDLE < 0  && Check for error opening file
	= MESSAGEBOX("Unable to Access",0,"Error")
	QUIT
ENDIF


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
If you use the second approach, you will need to check for the existence of the file BEFORE you try and open it - if it's not there, you will get a -1 in the the return value.

If you are checking for the existence of a file in VFP, remember that FILE() can give ambiguous results - even with a fully qualified path, VFP checks it's own search path!

So, use ADIR() instead

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Hi guys

Im trying to check if an excel file is open, meaning visible in your taskbar?
 
Im trying to check if an excel file is open, meaning visible in your taskbar?

Gosh. I know we're all geniuses here, and can answer just about any question your throw at us, but even the best of us would find it hard to have guessed that's what you wanted to know.

Look, I'm sorry about the sarcasm. But, really, if you had asked that in the first place, we could have avoided seven unnecessary posts in this thread, not to mention saving the time wasted by those who posted them.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Then you should check the file with FOPEN(filename,2), like Griffs second codesample.

It doesn't test for EXCEL having the file open, but it doesn't really matter, even if another application would have that file open, you can't reopen it with excel, then.

Bye, Olaf.
 
OK, having finished my rant, I'll try to answer your question:

This routine will search for any open window or document, and, if found, it will return .T.

Code:
FUNCTION CheckWindow
DECLARE INTEGER GetActiveWindow IN Win32API
DECLARE INTEGER GetWindow IN Win32API ;
	INTEGER hWnd, INTEGER nType	
DECLARE INTEGER GetWindowText IN Win32API ;
	INTEGER hWnd, STRING @cText, INTEGER nType	

lcTitle = "Excel"
* Change the above to any unique string that appears
* in the title bar of the window you are looking for		
	
hNext = GetActiveWindow()
llSuccess = .F.
* iterate through the windows in the stack
DO WHILE hNext<>0 
  cText = REPLICATE(CHR(0),80)
  GetWindowText(hNext,@cText,80)
	
  IF UPPER(lcTitle) $ UPPER(cText)
    llSuccess = .T.
    EXIT
  ENDIF
  hNext = GetWindow(hNext,2)
ENDDO
RETURN llSuccess

You just need to change lcTitle to something that includes your workbook's name, as it appears in the title bar of Excel.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I would use this:
Code:
IF ISITOPEN("c:\myfolder\myfile.xls")
	MESSAGEBOX("hello it's open",48,"Problem")
ELSE
	MESSAGEBOX("Excellent, it isn't open",48,"No Problem")
ENDIF

FUNCTION ISITOPEN
	PARAMETER m.FILENAME
	PRIVATE m.FILENAME,m.FILEHANDLE,m.FLG
	m.FILEHANDLE = -1
	m.FLG = .T.
	IF MYFILE(m.FILENAME)
		m.FILEHANDLE = FOPEN(m.FILENAME,2)
	ELSE
		m.FILEHANDLE = FCREATE(m.FILENAME)
	ENDIF
	IF m.FILEHANDLE < 0  && Check for error opening file
		m.FLG = .F.
	ELSE
		FCLOSE(m.FILEHANDLE)
	ENDIF
RETURN(m.FLG)

FUNCTION MYFILE
	PARAMETER m.FILENAME
	PRIVATE m.FILENAME,m.FLG
	m.FLG = .F.
	IF !EMPTY(m.FILENAME)
		IF ADIR(TMPDIRFILES,m.FILENAME) > 0
			m.FLG = .T.
		ENDIF
	ENDIF
	RETURN(m.FLG)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Checking the windows will not help if the copy of excel is hidden - i.e. opened with automation and crashed...

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Checking the windows will not help if the copy of excel is hidden - i.e. opened with automation and crashed..

True. But he didn't say anything about using Automation. He said "visible in your taskbar".

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
true... he did in the end!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
@Mike I am Im sorry I do have a bad habbit of not explaining things properly. Yes it is automation

@Griff, your third suggestion it works, well its working backwards meaning, if its not open, it will say it is open, and if it is open it will say its not?
 
Yeah, of course, I should have swapped the flag around!



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Meaning .T. should be false and .F. should be true for FLG?
 
Yes, swapt .T. with .F., but only in the ISITOPEN function, the MYFILE function is ok.

Or simply change the function name from ISITOPEN to ISITCLOSED

Bye, Olaf.
 
Exactly

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Thanks works like a beauty...

thank you guys you are Genius's
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top