Functions for file names in FoxPro


FoxPro has a wide range of text-handling functions (more) but there are also six functions which are specifically designed for working with file and path names. All were part of the FoxTools library (FoxTools.fll) in earlier versions of Foxpro but are now part of the main language.

AddBS()

AddBS() adds a final backslash to a path if it does not already end in a backslash. For example:

AddBS("C:\Microsoft Visual FoxPro 9")

returns

C:\Microsoft Visual FoxPro 9\

but if the path being passed in already ends in a backslash:

AddBS("C:\Microsoft Visual FoxPro 9\")

returns

C:\Microsoft Visual FoxPro 9\

Note that you can pass a path with forward slashes into AddBS() but will always add a backslash, even if the path already ends with a forward slash.

AddBS() is particularly useful when you are processing pathnames or assembling a path from a number of separate components. If you use the GetDir() function to let the user select a folder from directory tree then you will always get the path with a final backslash. If you are using another technique to enter paths or importing the paths from another application then AddBS() lets you add a consistent backslash to these values.

JustDrive()

JustDrive() returns the drive letter from a string containing the full path to a file or directory. For example:

JustDrive("C:\Microsoft Visual FoxPro 9\vfp9.exe")

returns

C:

Note that JustDrive() does not check that the drive actually exists, it just processes the string passed in as a parameter. JustDrive() will return the first two characters from the string if the second of these is a colon. If the second character is not a colon then it will return an empty string. As an example of the problems that this can cause:

JustDrive("\\XP_Server\Shipped")

returns

<empty string>

JustDrive() is of limited use. The only time that we have found it useful is when an application has had to run on a variety of machines, each with the server mapped to a different drive letter.

JustExt()

JustExt() returns the extension from a string containing a file name. For example:

JustExt("vfp9.exe")

returns

exe

If the file has no extension, or if the file name ends in a period, then an empty string will be returned.

As with JustDrive() above, JustExt() does not look at the file system or check that the parameter is a properly formed name or path. All that it does is look for the final period in a string and return the characters after that period.

JustFName()

JustFName() returns the name of a file complete with its extension.

JustFName("C:\Microsoft Visual FoxPro 9\vfp9.exe")

returns

vfp.exe

JustPath()

JustPath() returns the path to a file or folder.

JustPath("C:\Microsoft Visual FoxPro 9\vfp9.exe")

returns

C:\Microsoft Visual FoxPro 9

JustPath will also accept the name of a folder and will return the path to that folder. There is however a subtle difference in its behaviour depending on whether the folder name ends with a backslash. If there is no backslash then you get the path to that folder:

JustPath("C:\Microsoft Visual FoxPro 9")

returns

C:\

But if there is a already a final backslash then you get the same folder name returned with the backslash removed:

JustPath("C:\Microsoft Visual FoxPro 9\")

returns

C:\Microsoft Visual FoxPro 9

Unlike JustDrive(), JustPath will operate on network drives and on paths containing forward slashes:

JustPath("\\ServerXP\Shipped\January.zip")

returns

\\ServerXP\Shipped

and

JustPath("http://www.alvechurchbells.org.uk/default.html")

returns

http://www.alvechurchbells.org.uk

JustStem()

JustStem() returns the stem of a file name, the name with the period and extension removed. It can accept a filename with or without a path:

JustStem("C:\Microsoft Visual FoxPro 9\vfp9.exe")

returns

vfp9

JustStem() can accept a folder name instead of a filename but it is similar to JustPath() in the way that its behaviour changes depending on whether or not the folder name ends in a backslash.

JustStem() is useful when you are copying files or exporting data. If you are sending data from a table to an Excel file then you can use it to build the name of an xls file which matches the table name:

*-- Get the name of the table behind the current alias
*-- then extract its stem.
lcTableName = Dbf()
lcStem = JustStem(lcTableName)

*-- Now build the name of the xls and export the data.
lcXlsName = lcStem + ".xls"
Copy To &lcXlsName Type Xls

This is a simple example which just dumps the xls file into the current folder. A practical example would read the name of the export folder from a table or ask the user where they wanted to store the exported data.


Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map