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 |
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! |
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! |
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. |
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 |