xHarbour Reference Documentation > Command Reference |
Changes the name of a file.
RENAME <cOldFile> TO <cNewFile>
The RENAME command changes the name of a file. The file <cOldFile> is searched in the current directory only, unless a full qualified file name including drive and path is specified. Directories specified with SET DEFAULT and SET PATH are ignored by RENAME.
If <cNewFile> is specified as a full qualified file name and its directory differs from the one of <cOldFile>, the source file is moved to the new directory and stored under the new file name.
When the new file either exists or is currently open, RENAME aborts the operation. Use the File() function to test for the existence of <cNewFile>.
A file must be closed before attempting to rename it.
Important: Database files with memo fields have accompanying memo files. Memo files must be renamed along with their database files.
See also: | COPY FILE, CurDir(), ERASE, FErase(), FError(), File(), FRename() |
Category: | File commands |
Source: | rtl\philes.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example shows a user defined function that renames a database file // along with its memo file. PROCEDURE Main ? File( "Customer.dbf" ) // result: .T. ? File( "Customer.fpt" ) // result: .T. ? File( "Cust2005.dbf" ) // result: .F. ? File( "Cust2005.fpt" ) // result: .F. DbRename( "Customer.dbf", "Cust2005.dbf" ) ? File( "Customer.dbf" ) // result: .F. ? File( "Customer.fpt" ) // result: .F. ? File( "Cust2005.dbf" ) // result: .T. ? File( "Cust2005.fpt" ) // result: .T. RETURN FUNCTION DbRename( cOldFile, cNewFile ) LOCAL cOldName := SubStr( cOldFile, 1, Rat( ".", cOldFile ) ) LOCAL cNewName := SubStr( cNewFile, 1, Rat( ".", cNewFile ) ) LOCAL cMemoExt, n LOCAL aMemoExt := { "DBT", "FPT", "SMT" } IF ! File( cOldFile ) .OR. File( cNewFile ) RETURN .F. ENDIF FOR n:=1 TO 3 IF File( cOldName + aMemoExt[n] ) cMemoExt := aMemoExt[n] EXIT ENDIF NEXT RENAME (cOldFile) TO (cNewFile) IF ! Empty(cMemoExt) .AND. File( cOldName+cMemoExt ) RENAME (cOldName+cMemoExt) TO (cNewName+cMemoExt) ENDIF RETURN .T.
http://www.xHarbour.com