Copy File, Delete File, Erase, Rename

These commands let you do file maintenance without leaving FoxPro. They all do what their names suggest. Delete File and Erase do the same thing—get rid of an existing file.

Usage

COPY FILE SourceFile TO DestFile
DELETE FILE [ FileName | ? ] [ RECYCLE ]
ERASE [ FileName | ? ] [ RECYCLE ]
RENAME OldFileName TO NewFileName
As with any potentially destructive command, be careful when you use these, especially across drives or directories. Starting in VFP 5, DELETE FILE and ERASE have the potential to be very destructive, in fact, since they accept DOS wildcards and let you delete multiple files at once. In addition, they ignore the setting of SET SAFETY—once you issue the command, the file is history. Invoking these commands with the "?" or blank parameter brings up a standard "Select file" dialog. Watch out for this in applications, because the latest incarnations of that dialog give users an awful lot of power via the right-click menu.COPY FILE respects SET SAFETY. RENAME ignores SET SAFETY, but it won't let you give a file the name of an existing file. Neither of these commands accepts the "?" to let the user choose a file. However, COPY FILE doesn't scream if you specify ? for the DestFile; it also doesn't do anything.DELETE FILE and ERASE don't care whether the file you specify exists. If you give them a nonexistent filename, they just do nothing and go merrily on their way. This is a trap waiting to catch you because it means a typo in a program might keep a file from being deleted with you none the wiser. (Actually, with TALK ON, you do get some feedback to the status bar, but that doesn't help much in a program.) Help warns you about renaming tables contained in a database with RENAME. Heed this warning—you'll only create trouble for yourself if you don't. This is what RENAME TABLE is for.

Example

* Copy a file to make a backup
COPY README.TXT TO README.BAK
 
* Change the current transaction file into
* a history file.
cHistFile = "XACT"+TRANSFORM(YEAR(DATE()),"9999")
RENAME XActions.DBF TO (cHistFile+".DBF")
 
* Clean up a temp file. Either of the following will do
DELETE FILE TEMP.TXT
ERASE TEMP.TXT

See Also

ADir(), Copy To, Delete, File(), Filer, Rename Table, Set Safety


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .