Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

CD, ChDir, MD, MkDir, RD, RmDir, SET DEFAULT, Set(“Default”), Set(“Directory”), DefaultFilePath

When CD, MD and RD were added to Visual FoxPro, they may have been our favorite new commands (they were certainly the easiest to learn!). They do the same thing as in DOS (change directories, create a new directory, and delete a directory, respectively), but you don’t have to shell out to DOS to do it. SET DEFAULT is the traditional FoxPro equivalent of CD. DefaultFilePath is the automation server equivalent of SET DEFAULT—it lets you set the default directory and find out what it is.

Usage

CD | CHDIR [ ? | cPath ]
MD | MKDIR cPath
RD | RMDIR cPath
SET DEFAULT TO [ cPath ]
cDrive = SET( "DEFAULT" )
cPath = SET( "DIRECTORY" )
appApplication.DefaultFilePath = cPath
cPath = appApplication.DefaultFilePath

We can’t see any reason to use the long versions of these names, so it’s CD, MD and RD from here on out.

CD is almost equivalent to SET DEFAULT. Both change FoxPro’s default directory. That’s where anything you create gets saved unless you specify otherwise. Unlike the same-named DOS command, FoxPro’s CD can handle a drive as well as a directory. In FoxPro/DOS, there’s a bug where changing the drive with SET DEFAULT doesn’t remember to tell DOS about the change. This can lead to some rather nasty consequences. Fortunately, neither FoxPro 2.x for Windows nor Visual FoxPro shares this bug.

Our favorite form of CD is CD ?, which lets us find the directory we want, then change to it. (In fact, you can even shorten it to CD? if you want.) Even better, IntelliSense in VFP 7 remembers the last several directories, which appear in a popup menu when you type the space after CD.

Both CD and SET DEFAULT can't see directories with the System attribute. Attempting to change to such a directory generates an "Invalid path or file name" error. Oddly, though, all of these commands can deal with directories below a system directory in the tree. So, you can create a subdirectory of a system directory and move to that subdirectory, but while you're there, you can't issue CD .. to go to the parent. How odd!

The difference between CD and SET DEFAULT comes when you don’t pass a path. In that case, CD returns the current default directory, though in a different form than either SYS(2003) or CURDIR(). The path includes the drive, is in all lowercase and does not include a final “\”. The output goes to the active window, so be careful with this one. We use it all the time in development, but would never put it in an app. There’s one oddity here—using the long form of the CHDIR command without a path gets you a “Command is missing required clause” error.

With no path, however, the behavior of SET DEFAULT TO depends on whether you're currently on the drive containing the VFP startup directory. If so, the command doesn't do anything at all. If you're on a different drive, you're changed to the root directory of the drive containing the VFP startup directory. Weird!

For some reason, SET(“DEFAULT”) has never been enhanced to keep up with the times. It still returns just the drive with no path. No extra parameters or anything to let you find the path. Of course, CURDIR() provides the path. Even better, the undocumented SET(“DIRECTORY”) gives you the drive and path as a single string.

DefaultFilePath is the Automation-server version of SET DEFAULT. It lets you find out and set the default directory when you’re addressing VFP as a server.

MD and RD are the really big wins here. CD is nice, but we always had SET DEFAULT. Now, you can create and destroy directories from within your application. What power! Your application can create the directories it needs to store data, create temporary directories and then clean up, and so on.

The same rules apply as in DOS, so the directory you’re removing must be empty and you can’t be parked there.

By the way, we’ve always thought that the person who named CD and MD must have majored in confusion. It took us years to remember they stood for “change directory” and “make directory” rather than “create directory” and “move to directory.”

Example

MD TEST  && Create a TEST subdirectory of current directory.
CD TEST  && Change to it.
* do some work
* then delete the files
* then
CD ..
RD TEST  && Get rid of it.

* Change to the directory I want to work in.
CD H:\HACKER
? _VFP.DefaultFilePath  && Returns h:\hacker

CD and SET DEFAULT have a bug under the 32-bit versions of Windows (which, of course, means it always shows up in VFP 5 and later). In FoxPro 2.x and older versions, SET DEFAULT allowed you to change drives without changing directories. That is, you could SET DEFAULT TO <drive letter> and it didn't change the current directory on that drive. In VFP 3 under the 16-bit versions of Windows, you can do the same thing with either SET DEFAULT or CD. However, under 32-bit Windows, if you change to a subdirectory, then change to another drive, then back to the first drive, you're no longer in the subdirectory on that drive.

Here’s an example that’ll give you different results in 32-bit Windows than in Win 3.1 and Windows for Workgroups:

CD D:
CD \VFP\SAMPLES
CD C:
CD D:
* Now where are we?
? CURDIR()      && "D:\" on 32-bit versions; "D:\VFP\SAMPLES" on 3.1 and 3.11

Regardless of the platform, both MD and RD have a problem with relative addressing on another drive. (We suspect this is related to the previous bug, however.) If you try to create a new directory as a subdirectory of the current directory on another drive, it gets created at the root anyway. For example:

* This example only demonstrates the bug under 3.1 and 3.11,
* but it exists in the other versions, too.
CD H:\HACKER    && Set default on other drive.
CD F:           && Back to work.
? CURDIR("H:")  && Returns "\HACKER\"
MD H:SHOWBUG    && Should be a sub-directory of Hacker
CD H:SHOWBUG    && but it's not. This doesn't work; you need:
CD H:\SHOWBUG   && This works!

The new directory created in the example is H:\SHOWBUG instead of H:\HACKER\SHOWBUG. The workaround is to explicitly reference the full path:

MD ("H:\HACKER\SHOWBUG")

What a pain! We hope they fix it soon, but we’re not holding our breath.

See Also

CurDir(), GetDir(), Run, Sys(5), Sys(2003)