File(), Directory()

These two handy functions check whether a file or directory exists. If there's any doubt in your mind before trying to open a file, or if you want to make sure there isn't an output file by that name and offer the user the option of supplying a new name or overwriting, these are the functions for you.

Usage

lFileExists = FILE( cFilePath )
lDirExists = DIRECTORY( cDirectory )
FILE() returns a logical value if the specified file name exists in the specified directory. So far, so good.

If no directory is specified, in addition to the current directory, FoxPro also searches the FoxPro PATH (not the DOS path) set with the SET PATH statement, and returns .T. if the file is found anywhere along that path. It can be argued that "It's a feature, not a bug," as we might in the section by that name, but this one trips us up often. To specify that FILE()searches only the current directory, you must explicitly specify at least the directory path name, starting at the root of the drive. So you'll probably want to type this command as
FILE(CURDIR() + cFileName) in most cases.


Note that when you use FILE(cPath + cFilename) within an EXE, it returns .T. if the file exists in the EXE, even when the file doesn't exist in the specified directory. You have to use ADIR() to see if the file exists in the directory.DIRECTORY() tells us whether a directory exists. The parameter can be a relative path ("..\MyDir") or explicit from the root of the drive (" C:\MyDir\MySubDir") and isn't required to end with a slash. If one is there, it's ignored. DIRECTORY()supports UNC names.

Example

lOutFileHere = FILE("OutPut.PRN")
lDirExists = DIRECTORY("C:\Temp")

See Also

ADir(), CurDir(), Set Path


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .