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.
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.
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.
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."
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
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
* 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.