Command RENAME Foundation

Changes the name of a file.

Syntax
RENAME <cOldName> TO <cNewName>
Parameters
<cOldName>
<cOldName> specifies the name of a file to rename. The name must include the drive and path if they are necessary. The file name can be specified either as a literal file name or as a character expression in parentheses.
<cNewName>
<cNewName> specifies the new name for the file. The name must include the drive and path if they are necessary. The file name can be specified either as a literal file name or as a character expression in parentheses.
Description

The file command RENAME renames a file. The file is renamed only when <cOldFile> and <cNewFile> contain valid file names. If the operation fails, the file error can be determined using the error code returned by the function FError().

The command does not consider either the PATH setting or the path specified with DEFAULT. <cOldName> and <cNewName> must be complete file names including the drive and path. When the file name does not contain drive or path, the current directory is searched for <cOldName>.

If the original directory is not the same as the target directory, the file is moved to the target directory. If a file already exists with the file name<cNewName>, RENAME fails.

The functional equivalent of the command RENAME is the function FRename().

Before a file is renamed with RENAME, it must first be closed if it is open. When a DBF file containing memo fields is renamed, the matching DBT file must also be renamed. Otherwise, the DBF file cannot be opened with USE.

Examples
RENAME
// In the example, a DBF file and its memo file are renamed, 
// showing different ways to call the RENAME command. 

PROCEDURE Main 
   LOCAL cOldMemoFile := "CUST.DBT" 
   LOCAL cNewMemoFile := "CUSTOMER.DBT" 

   ? File( "CUST.DBF" )                    // result: .T. 
   ? File( cOldMemoFile )                  // result: .T. 

   RENAME Cust.dbf TO Customer.dbf         // literal file name 
   RENAME (cOldMemoFile) TO (cNewMemoFile) // specify using variables 

   ? File( "CUST.DBF" )                    // result: .F. 
   ? File( cOldMemoFile )                  // result: .F. 

RETURN 

User defined command RENAME DBF
// The example shows a user defined command which renames 
// DBF files and the corresponding memo file if it exists. 
// The file names, but not the file extensions, are specified. 

#command RENAME DBF <(OldFile)> TO <(NewFile)> ; 
      => ; 
         IF File(<(OldFile)>+".DBF") ;; 
            RENAME (<(OldFile)>+".DBF") TO (<(NewFile)>+".DBF") ;; 
            IF FError()==0 .AND. File(<(OldFile)>+".DBT") ;; 
               RENAME (<(OldFile)>+".DBT") TO (<(NewFile)>+".DBT") ;; 
            ENDIF ;; 
         ENDIF 

PROCEDURE Main 

   ? File( "CUST.DBF" )              // result: .T. 
   ? File( "CUST.DBT" )              // result: .T. 

   RENAME DBF Cust TO Customer 

   ? File( "CUST.DBF" )              // result: .F. 
   ? File( "CUST.DBT" )              // result: .F. 

   ? File( "CUSTOMER.DBF" )          // result: .T. 
   ? File( "CUSTOMER.DBT" )          // result: .T. 

RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.