RAD Studio |  |

Renaming a File
See Also
Collapse All

To change a file name, use the RenameFile function:

Copy Code

    function RenameFile(const OldFileName, NewFileName: string): Boolean;

Copy Code

    extern PACKAGE bool __fastcall RenameFile(const AnsiString OldName, const AnsiString NewName);

RenameFile changes a file name, identified by OldFileName, to the name specified by NewFileName. If the operation succeeds, RenameFile returns True. If it cannot rename the file (for example, if a file called NewFileName already exists), RenameFile returns False. For example:

Copy Code

    if not RenameFile('OLDNAME.TXT','NEWNAME.TXT') then
      ErrorMsg('Error renaming file!');

Copy Code

    if (!RenameFile("OLDNAME.TXT","NEWNAME.TXT"))
      ErrorMsg("Error renaming file!");

You cannot rename (move) a file across drives using RenameFile. You would need to first copy the file and then delete the old one.

Note: RenameFile in the runtime library is a wrapper around the Windows API MoveFile function, so MoveFile will not work across drives either.
See Also

Finding a File

Deleting a File

File Date-time Routines

Copying a File

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!