IsDir() — A Macro-Function

by Bowen Moursund
August 15, 2001

When programming your dB2K application, you may find it necessary to be able to determine if a file directory actually exists. The following solution utilizes the FUNIQUE( ) function and also illustrates the creation of a macro-function using the dB2K preprocessor.

The dBL FUNIQUE() function is typically used to generate a unique name for a temporary file by passing it a string parameter containing a path and file name skeleton, for example:

  cTempFile = fUnique( "C:\Temp\Temp????.???" )

However, if the specified directory does not exist, fUnique() returns an empty string. By taking advantage of this behavior, you can find out if the C:\Temp directory exists:

  bIsTempDir = not empty(funique("C:\Temp\Temp????.???"))

Since you don't want to modify and type this entire statement every time you need to know if a directory exists, a more useful, reusable solution is to create a macro-function. Why a macro-function rather than a regular function? When the preprocessor encounters a function call that matches a defined macro-function, it replaces the function call with the replacement text, inserting the arguments of the function call into the replacement text. In other words, it's almost as if you did type the entire statement directly into your program - the overhead associated with the use of an actual function is avoided. Here's how to use a macro-function designed for our purpose:

  #define isDir(cDir) (not empty(funique(cDir+"\isdir???.???")))
  // If the directory doesn't exist
  if not isDir("C:\Backup")
     // Then create it
     mkdir "C:\Backup"
  copy table MyTable.Dbf to C:\Backup\MyTable.Dbf

Don't forget to paste in your macro-function #definition at the top of the source files where it might be utilized. Better yet, as you develop new macro-functions, collect them into a single file. With a single preprocessor directive, the entire collection can be inserted into your source files:

  #include MyMacroFunctions.h

See the dB2K Online Help for more information on the preprocessor, #define, and #include. Enjoy!