rename

Description

Renames a file or directory.

#include <stdio.h> Required for ANSI compatibility  
#include <io.h> Use either IO.H or STDIO.H  

int rename( const char *oldname, const char *newname );

oldname Pointer to old name  
newname Pointer to new name  

Remarks

The rename function renames the file or directory specified by oldname to the name given by newname. The old name must be the path name of an existing file or directory. The new name must not be the name of an existing file or directory.

The rename function can be used to move a file from one directory to another by giving a different path name in the newname argument. However, files cannot be moved from one device to another (for example, from drive A to drive B). Directories can only be renamed, not moved.

Return Value

The rename function returns 0 if it is successful. On an error, it returns a nonzero value and sets errno to one of the following values:

Value Meaning

EACCES File or directory specified by newname already exists or could not be created (invalid path); or oldname is a directory and newname specifies a different path.
ENOENT File or path name specified by oldname not found.
EXDEV Attempt to move a file to a different device.

Compatibility

Standards:ANSI

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Example

/* RENAMER.C: This program attempts to rename a file named RENAMER.OBJ to

* RENAMER.JBO. For this operation to succeed, a file named RENAMER.OBJ

* must exist and a file named RENAMER.JBO must not exist.

*/

#include <stdio.h>

void main( void )

{

int result;

char old[] = “RENAMER.OBJ”, new[] = “RENAMER.JBO”;

/* Attempt to rename file: */

result = rename( old, new );

if( result != 0 )

printf( “Could not rename '%s'\n”, old );

else

printf( “File '%s' renamed to '%s'\n”, old, new );

}

Output

File 'RENAMER.OBJ' renamed to 'RENAMER.JBO'