Visual FoxPro functions for file names

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 originally part of the FoxPro Link Library (FoxTools.fll) in earlier versions of Foxpro but are now part of the main language.

These functions are described below:

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")

would return the path with a final backslash:

C:\Microsoft Visual FoxPro 9\

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

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

then the function would not add another backslash but would return:

C:\Microsoft Visual FoxPro 9\

Note that you can pass a path with forward slashes into AddBS() but the function 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 the directory tree then you will always get the path with a final backslash. If you are using another technique to enter paths or if you are importing the paths from another application then AddBS() lets you add a consistent backslash to these values and avoid errors when you try to assemble a pathname and file without backslashes.

JustDrive()

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

For example:

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

would return the drive:

C:

Note that JustDrive() does not check whether the drive actually exists, it just processes the string passed in as a parameter. The JustDrive() function 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 the function will return an empty string. As an example of the problems that this can cause, the use of a valid network path such as:

JustDrive ("\\XP_Server\Shipped")

would return:

<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 of which had 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")

would return

exe

If the file has no extension, or if the file name just 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 that follow it.

JustFName()

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

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

would return:

vfp.exe

JustPath()

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

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

would return:

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")

would return:

C:\

But if there is a already a final backslash then you would 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 safely on network drives and on paths containing forward slashes:

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

would return:

\\ServerXP\Shipped

and

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

would return:

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")

would return:

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 this function 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.

Related Items

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle files which defeat the STRTOFILE and FILETOSTR functions.

Read More

FoxPro functions for manipulating text

Foxpro commands and functions for text

Read More

FoxPro Gotcha

Common mistakes in Visual FoxPro

Read More

Close a low level file in FoxPro

FoxPro file handling - use FCLOSE() to close a low level file

Read More

Create a low level file with FoxPro

FoxPro file handling - use FCREATE() to create a low level file

Read More