Rename a file or directory

From CodeCodex

Revision as of 19:46, 24 December 2010 by 211.2.129.92 (Talk)

Implementations

C

#include <stdio.h>
rename("/path/to/old/file", "/path/to/new/file");

Erlang

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

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
    }

OCaml

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

Perl

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

Python

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

Ruby

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

Seed7

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