RAD Studio
ContentsIndex
PreviousUpNext
Renaming a File

To change a file name, use the RenameFile function:

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

 

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:

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

 

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.

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