Prev

Perl Notes

Next

Chapter 8.6 Renaming a File

Sometimes it becomes necessary to change the name of a file.

To rename a file:

  1. Type rename(name, where name is the current filename for the file, or a scalar variable that refers to the filename.
  2. Type newname, where newname is the desired name that you want the file to have. You may also use a scalar variable that contains the new name.
  3. Type ) to complete the function.
  4. Type ; to complete the line.

rename("../myfile.txt", "../yourfile.txt") || &ErrorMessage;

The next example moves myfile.txt into the save directory and renames it to $filename.
$filename="yourfile.txt";
rename("../myfile.txt", "../save/" . $FILENAME) || &ErrorMessage;


Prev Home Next