To rename a file or folder in Ruby you can use the following snippet.
Sample Ruby
begin File.rename("/path/to/oldfile", "/path/to/newfile") rescue Exception => e # the exception your way end
To rename a file or folder in Ruby you can use the following snippet.
begin File.rename("/path/to/oldfile", "/path/to/newfile") rescue Exception => e # the exception your way end
To rename a file or folder in Java you can use the following snippet.
File source = new File("oldDirectoryOrFilePath"); File target = new File("newDirectoryOrFilePath"); boolean done = source.renameTo(target); if (!done) { // renaming failed }
To rename a file or folder in Python you can use the following snippet.
import os os.rename("pathOld","pathNew") #file or folderpaths
To rename a file in c# or vb.net you need to Move it, there is no inbuild function that would just rename a file without moving.
The Move function can not overwrite a file and will throw a IOException if the file already exists, therefore we try to delete it first if it exists.
Sample C#
const string oldName = @"C:\OldFilename.txt"; const string newName = @"C:\NewFilename.txt"; File.Delete(newName); // First delete the new file if it exists, because it wont be overwritten by Move File.Move(oldName, newName); // Move the file and give it a new name
Sample VB.NET
Const oldName As String = "C:\OldFilename.txt" Const newName As String = "C:\NewFilename.txt" File.Delete(newName) ' First delete the new file if it exists, because it wont be overwritten by Move File.Move(oldName, newName) ' Move the file and give it a new name
Fore more informations see File.Move Method, File.Delete Method,IOException Class