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!

Colleagues! We are all using the b 3

Status
Not open for further replies.

IRABYY

Programmer
Apr 18, 2002
221
US
Colleagues!
We are all using the built-in function FILE() in VFP for checking the existence of certain file, ain't we? I found lately that it's not fit for the cases when we either do not know exactly the file name and/or extension, or need just to find out if a group of files with common name or extension exists. For instance, we want to check if any file with DBF extension exists in the directory lcSomePath, and we either do not know or do not care the file(s) name(s). Wouldn't it be nice to have the possibility to issue just
Code:
?FILE(lcSomePath + "*.DBF")
and get the result? But FILE() function does not take wildcards. Therefore, I wrote custom function FileX(), which essentially is a wrapper for ADIR() built-in function that DOES take wildcards. Here it is:
Code:
******************************************************************************************************************************
FUNCTION FileX(tcPath)
******************************************************************************************************************************
** Function Name : File eXtended
** Purpose       : Determines if the file with the given name (regardles of extension or wildcard characters) exists.
** Description   : Extended version of the standard FILE() function. Takes any extension or any legal wildcard.
** Parameter(s)  : Full path as string.
** Return        : Success as Boolean.
** Side Effect(s): Shan't be any.
** Notes:        : Generates no error messages on its own.
******************************************************************************************************************************
&& Parameter validation:
IF TYPE('tcPath') # "C"
   RETURN .F.
ENDIF
tcPath = ALLTRIM(tcPath)
IF EMPTY(tcPath)
   RETURN .F.
ENDIF

&& The following statement is for VFP 6.0 and higher
** tcPath = ADDBS(tcPath)
&& The followiing is for VFP 3.0 and earlier versions of FoxPro
**IF RIGHT(tcPath, 1) # "\"
**   tcPath = tcPath + "\"
**ENDIF
&& Un-comment whichever statement above fits your version if FoxPro

LOCAL ARRAY laFile[1]
LOCAL lnMax

lnMax = ADIR(laFile, tcPath)
RELEASE ALL LIKE laFile*

RETURN (lnMax > 0)
ENDFUNC
******************************************************************************************************************************
You are invited to copy-paste this function and use it any way you like, free of charge. [smile] I just thought it may be helpful in certain situations.
Any comments, or suggestions for refinements are quite welcome!

Regards,

Ilya
 
Ilya,
Nice routine. Just so you know, we always had ADDBS(), it's just before 6.0, you had to use it in FoxTools.FLL (all the way back to FPW 2.5!).

Rick
 
rgbean (Programmer):
we always had ADDBS(), it's just before 6.0, you had to use it in FoxTools.FLL

Didn't know that! Thanks!

Regards,

Ilya
 
Rick,

How do you use ADDBS() function in instance Ilya is talking about? I'd rather use simple method instead of copying the whole function, but do not see how this works???

THanks,
Igor.
 
A method that is as effective is:
Code:
LOCAL laDir[1]
....
IF 0<ADIR(laDir,'File?skeleton*.txt')
  * file(s) exist
ELSE
  * file doesn't exist
ENDIF
*optional:
RELEASE laDir
It avoids the function-call overhead, and is therefore faster. it takes more lines (1 more for LOCAL, and potentially another for RELEASE), and uses memory for more time (It doesn't use more memory... the array just exists longer, unless you use a RELEASE in 2 places. I usually just let the array go out of scope.)
----
FYI: another way to account for the VFP version differences:
Code:
#IF VAL(SUBSTR(VERSION(),15)) >= 6
  * The following statement is for VFP 6.0 and higher
  tcPath = ADDBS(tcPath)
#ENDIF
* #ELSE doesn't work in older FP versions
#IF VAL(SUBSTR(VERSION(),15)) < 6
  * The followiing is for VFP 3.0 and earlier versions of FoxPro
  IF RIGHT(tcPath, 1) # "\"
    tcPath = tcPath + "\"
  ENDIF
#ENDIF
 
Then there is always:

*!* Returns .T. if dbf file exists in lcPath
lcPath = "C:\"
?!EMPTY(SYS(2000,lcPath + "*.DBF"))


boyd.gif

craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top