Mega Code Archive

 
Categories / Delphi / Procedures
 

Rename - 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 Erase Erase a file RenameFile Rename a file or directory 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