Wiki Home

VFP Function File


Namespace: VFP
File( cFile ) -- Returns whether cFile can be found on the search path (SET PATH TO ...).

The documentation states that the SET PATH is only searched if cFile doesn't include a path, HOWEVER this is not true for if the path in cFile is on a different drive, the SET PATH is STILL SEARCHED! This has been reported to MS as a bug. It behaves this way in VFP v6 and VFP v7. I didn't test v3 or v5 or FP2.6.

Here's code to reproduce it:

* Place a blank floppy disk in A:
SET PATH TO
SET DEFAULT TO c:\
=StrToFile('Test','junk.txt')
?"This should return .T.:      FILE('junk.txt'):"
??FILE('junk.txt')             && Should return .T. ... It Does
?"This should return .F.:      FILE('a:\junk.txt'):"
??FILE('a:\junk.txt')          && Should return .F. ... It Does
SET PATH TO SET('defa')+CURDIR()
?"This should return .F.:      FILE('a:\junk.txt'):"
??FILE('a:\junk.txt')          && Should return .F. ... It Doesn't!
?"This should return .F.:      FILE('b:\junk.txt'):"
??FILE('b:\junk.txt')          && Should return .F. ... It Doesn't!
?"This should return .F.:      FILE('w:\junk.txt'):"
??FILE('w:\junk.txt')          && Should return .F. ... It Doesn't!

?"This should return .F.: FILE('c:\junk\junk.txt'):"
??FILE('c:\junk\junk.txt')          && Should return .F. ...


A workaround is to Wrap all calls to File().. eg:

FUNCTION FileReallyExists( pcFile )
LOCAL lcPATH
lcPath = SET('PATH')
SET PATH TO
llRet = File(pcFile)
SET PATH TO (lcPath)
RETURN llRet


As long as we are on the subject, lets talk about files included in an .exe?


Another workaround is to use the sys(2000) function. It does not search the path.

I prefer using ADIR() instead
FUNCTION FileExists(tcFile)
LOCAL laFiles[1]
RETURN (ADIR(laFiles, tcFile) > 0)


FUNCTION FileExists(cFile)
  DECLARE INTEGER GetFileAttributes IN KERNEL32 STRING
  DECLARE INTEGER GetLastError IN kernel32
RETURN IIF((GetFileAttributes(cFile) = -1) AND (INLIST(GetLastError(), 2, 3)), .F., .T.)



Contributors: wgcs Ray Kirk Ed Leafe Yaroslav Shimanskyy

Category VFP Bugs Category VFP Functions
( Topic last updated: 2006.02.02 01:23:45 AM )