DelphiBasics
Rename
Procedure
Rename a file System unit
 procedure Rename(var FileHandle; NewFileName string | PChar);
Description
The Rename procedure renames a file given by FileHandle to a new name NewFileName.
 
The file must have been assigned the given FileHandle using the AssignFile routine.
Notes
Use RenameFile when you want to rename a file without needing to assign it.
Related commands
EraseErase a file
RenameFileRename a file or directory
 Download this web site as a Windows program.




 
Example code : Create a file, rename it, and then reopen it with the new name
var
  myFile : TextFile;
  text   : string;

begin
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');
  ReWrite(myFile);

  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');

  // Close the file
  CloseFile(myFile);

  // Try to rename the file - ensure that no such file exists first!
  DeleteFile('NewName.txt');
  Rename(myFile, 'NewName.txt');

  // Now read the file
  AssignFile(myFile, 'NewName.txt');
  ReSet(myFile);

  // Display the file contents
  while not Eof(myFile) do
  begin
    ReadLn(myFile, text);
    ShowMessage(text);
  end;

  // Close the file for the last time
  CloseFile(myFile);
end;
Show full unit code
  Hello
  World
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page