Difference between revisions of "Rename a file or directory"

From CodeCodex

(C)
(Erlang)
Line 4: Line 4:
 
#include <stdio.h>
 
#include <stdio.h>
 
rename("/path/to/old/file", "/path/to/new/file");
 
rename("/path/to/old/file", "/path/to/new/file");
 +
</pre>
 +
 +
===Erlang===
 +
<pre>
 +
file:rename("/path/to/old/file", "/path/to/new/file").
 
</pre>
 
</pre>
  
Line 52: Line 57:
  
 
[[Category:C]]
 
[[Category:C]]
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]

Revision as of 19:46, 24 December 2010

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");