Unlock the Power of File Renaming
When it comes to managing files, one of the most essential functions is renaming. Whether you need to update a file’s name or move it to a different location, the rename()
function is here to help.
How the rename()
Function Works
This powerful function takes two arguments: oldname
and newname
. The oldname
parameter points to the string containing the current name of the file, including its path. The newname
parameter, on the other hand, points to the string containing the desired new name of the file, along with its new path.
What to Expect from the rename()
Function
So, what happens when you call the rename()
function? If the file is successfully renamed, the function returns a value of zero. However, if an error occurs, it returns a non-zero value.
Real-World Examples
Let’s take a closer look at how the rename()
function works in practice. In our first example, we’ll try to rename a file. If the file exists and is successfully renamed, the output will be:
File renamed successfully
But what if the file doesn’t exist? In that case, the output will be:
Error renaming file: No such file or directory
Moving Files with Ease
The rename()
function is not just limited to renaming files. You can also use it to move a file to a different location. Simply provide a different path for the new name of the file, and the function will take care of the rest.
In our second example, we’ll try to move a file to a new location. If the file is successfully moved, the output will be:
File moved successfully
And if the file doesn’t exist? You guessed it – the output will be:
Error moving file: No such file or directory
With the rename()
function, managing your files has never been easier. So why wait? Start streamlining your file management tasks today!