Rename a file or directory

From CodeCodex

Contents

[edit] Implementations

[edit] C


#include <stdio.h>

rename("/path/to/old/file", "/path/to/new/file");

[edit] Java


// File (or directory) with old name
File file = new File("oldname");

// File (or directory) with new name
File file2 = new File("newname");

// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
        // File was not successfully renamed
}

[edit] OCaml


Sys.rename "/path/to/old/file" "/path/to/new/file"

[edit] Perl


rename("/path/to/old/file", "/path/to/new/file");

[edit] Python


os.rename("/path/to/old/file", "/path/to/new/file")

[edit] Ruby

begin
  File.rename("/path/to/old/file", "/path/to/new/file")
rescue Exception => e
  # handle file rename error here
end