Rename a file: Difference between revisions

m
Fixed lang tags.
(→‎{{header|Python}}: Use OS-portable directory separator)
m (Fixed lang tags.)
Line 4:
 
=={{header|Ada}}==
<lang ada>with Ada.Directories; use Ada.Directories;
<lang ada>
with Ada.Directories; use Ada.Directories;
...
Rename ("input.txt", "output.txt");
Rename ("docs", "mydocs");
Rename ("/input.txt", "/output.txt");
Rename ("/docs", "/mydocs");</lang>
</lang>
The behavior depends on the concrete [[OS | operating system]] regarding:
* file name encoding issues;
Line 23 ⟶ 21:
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - ''get directory'' and'' grep in string'' not available in any library ... yet}} -->
Note: <tt>reidf</tt> does not appear to be included in [[ALGOL 68G]]. Also note that file names would be Operating System dependent.
<lang algolalgol68>main:(
main:(
PROC rename = (STRING source name, dest name)INT:
BEGIN
Line 49 ⟶ 46:
=={{header|AWK}}==
Awk allows to call operating system commands with the ''system()'' function. However, the awk script won't get its output, only the return code. But this task is simple enough for the trivial implementation to work:
<lang awk>$ awk 'BEGIN{system("mv input.txt output.txt"}'
$ awk 'BEGIN{system("mv docs mydocs")}'
$ awk 'BEGIN{system("mv /input.txt /output.txt")}'
$ awk 'BEGIN{system("mv docs mydocs")}'</lang>
 
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>FileMove, oldname, newname</lang>
FileMove, oldname, newname
</lang>
=={{header|C}}==
<lang c>#include <stdio.h>
Line 73 ⟶ 68:
=={{header|C++}}==
{{trans|C}}
<lang cpp>#include <cstdio>
#include <cstdio>
 
int main()
Line 82 ⟶ 76:
std::rename("/input.txt", "/output.txt");
std::rename("/docs", "/mydocs");
}</lang ada>
}
</lang>
 
=={{header|C sharp|C#}}==
Line 109 ⟶ 102:
 
=={{header|D}}==
<lang d>std.file.rename("input.txt","output.txt");
<lang d>
std.file.rename("input.txt","output.txt");
std.file.rename("/input.txt","/output.txt");
std.file.rename("docs","mydocs");
std.file.rename("/docs","/mydocs");</lang>
</lang>
 
=={{header|DOS Batch File}}==
Line 130 ⟶ 121:
 
=={{header|Forth}}==
<lang forth> s" input.txt" s" output.txt" rename-file throw
s" /input.txt" s" /output.txt" rename-file throw</lang>
 
=={{header|Groovy}}==
Using File
<lang groovy>['input.txt':'output.txt', 'docs':'mydocs'].each { src, dst ->
<lang groovy>
['input.txt':'output.txt', 'docs':'mydocs'].each { src, dstdir ->
new File("$dir/$src").renameTo(new File("$dir/$dst"))
['.', ''].each { dir ->
}
new File("$dir/$src").renameTo(new File("$dir/$dst"))
}</lang>
}
}
</lang>
 
Using Ant
<lang groovy>['input.txt':'output.txt', 'docs':'mydocs'].each { src, dst ->
<lang groovy>
['input.txt':'output.txt', 'docs':'mydocs'].each { src, dstdir ->
new AntBuilder().move(file:"$dir/$src", toFile:"$dir/$dst")
['.', ''].each { dir ->
}
new AntBuilder().move(file:"$dir/$src", toFile:"$dir/$dst")
}</lang>
}
}</lang>
 
=={{header|Haskell}}==
<lang haskell> import System.IO
import System.Directory
 
main = do
renameFile "input.txt" "output.txt"
renameDirectory "docs" "mydocs"
renameFile "/input.txt" "/output.txt"
renameDirectory "/docs" "/mydocs"</lang>
 
=={{header|J}}==
Line 174 ⟶ 162:
'kernel32 MoveFileA i *c *c' 15!:0 y;x
end.
)</lang>
)
</lang>
 
Useage:
<lang j> 'output.txt' frename 'input.txt'
'/output.txt' frename '/input.txt'
'mydocs' frename 'docs'
'/mydocs' frename '/docs'</lang>
</lang>
 
=={{header|Java}}==
<lang java> import java.util.File;
public class FileRenameTest {
public static boolean renameFile(String oldname, String newname) {
// 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);
return sucess;
}
public static void test(String type, String oldname, String newname) {
System.out.println("The following " + type + " called " + oldname +
( renameFile(oldname, newname) ? " was renamed as " : " could not be renamed into ")
+ newname + "."
);
}
public static void main(String args[]) {
test("file", "input.txt", "output.txt");
test("file", File.seperator + "input.txt", File.seperator + "output.txt");
test("directory", "docs", "mydocs");
test("directory", File.seperator + "docs" + File.seperator, File.seperator + "mydocs" + File.seperator);
}
}</lang>
 
=={{header|JavaScript}}==
Line 222 ⟶ 208:
 
=={{header|Mathematica}}==
<lang Mathematica> SetDirectory[NotebookDirectory[]]
RenameFile["input.txt", "output.txt"]
RenameDirectory["docs", "mydocs"]
SetDirectory[$RootDirectory]
RenameFile["input.txt", "output.txt"]
RenameDirectory["docs", "mydocs"]</lang>
 
=={{header|MAXScript}}==
MAXScript has no folder rename method
<lang maxscript>-- Here
renameFile "input.txt" "output.txt"
-- Root
renameFile "/input.txt" "/output.txt"</lang>
 
=={{header|Objective-C}}==
Line 247 ⟶ 233:
 
=={{header|OCaml}}==
<lang ocaml> Sys.rename "input.txt" "output.txt";;
Sys.rename "docs" "mydocs";;
Sys.rename "/input.txt" "/output.txt";;
Sys.rename "/docs" "/mydocs";;</lang>
 
=={{header|Pascal}}==
Line 277 ⟶ 263:
 
=={{header|Perl}}==
<lang perl> use File::Copy qw(move);
use File::Spec::Functions qw(catfile rootdir);
# here
move 'input.txt', 'output.txt';
move 'docs', 'mydocs';
# root dir
move (catfile rootdir, 'input.txt'), (catfile rootdir, 'output.txt');
move (catfile rootdir, 'docs'), (catfile rootdir, 'mydocs');</lang>
 
=={{header|PHP}}==
Line 296 ⟶ 282:
=={{header|Pop11}}==
 
<lang pop11>sys_file_move('inputs.txt', 'output.txt');
sys_file_move('docs', 'mydocs');
sys_file_move('/inputs.txt', '/output.txt');
sys_file_move(/'docs', '/mydocs');</lang>
 
Note that notion of the root of filesystem is Unix specific, so above we
Line 305 ⟶ 291:
 
=={{header|PowerShell}}==
<lang powershell> Rename-Item input.txt output.txt
 
# The Rename-item has the alias ren
ren input.txt output.txt</lang>
 
=={{header|Python}}==
 
<lang python>import os
 
import os
os.rename("docsinput.txt", "mydocsoutput.txt")
os.rename("input.txtdocs", "output.txtmydocs")
 
os.rename("docs", "mydocs")
os.rename(os.sep + "docsinput.txt", os.sep + "mydocsoutput.txt")
os.rename(os.sep + "input.txtdocs", os.sep + "output.txtmydocs")</lang>
os.rename(os.sep + "docs", os.sep + "mydocs")
</lang>
 
=={{header|R}}==
Line 337 ⟶ 321:
The <tt>FileUtils#move</tt> method has some more flexibility than the core <tt>File#rename</tt> method (not really demonstrated here).
 
<lang ruby> require 'fileutils'
moves = { "input.txt" => "output.txt", "/input.txt" => "/output.txt", "docs" => "mydocs","/docs" => "/mydocs"}
moves.each{ |src, dest| FileUtils.move( src, dest, :verbose => true ) }</lang>
 
=={{header|Slate}}==
<lang slate>(File newNamed: 'input.txt') renameTo: 'output.txt'.
<lang slate>
(File newNamed: 'input.txt') renameTo: 'output.txt'.
(File newNamed: '/input.txt') renameTo: '/output.txt'.
(Directory newNamed: 'docs') renameTo: 'mydocs'.
(Directory newNamed: '/docs') renameTo: '/mydocs'.</lang>
</lang>
 
=={{header|Smalltalk}}==
Line 358 ⟶ 340:
 
=={{header|Standard ML}}==
<lang sml>OS.FileSys.rename {old = "input.txt", new = "output.txt"};
OS.FileSys.rename {old = "docs", new = "mydocs"};
OS.FileSys.rename {old = "/input.txt", new = "/output.txt"};
OS.FileSys.rename {old = "/docs", new = "/mydocs"};</lang>
 
=={{header|Tcl}}==
Line 378 ⟶ 360:
 
=={{header|Toka}}==
<lang toka>needs shell
" input.txt" " output.txt" rename
" /input.txt" " /output.txt" rename
 
" docs" " mydocs" rename
" /docs" " /mydocs" rename</lang>
 
=={{header|UNIX Shell}}==
<lang bash>mv input.txt output.txt
mv /input.txt /output.txt
mv docs mydocs
mv /docs /mydocs</lang>
 
=={{header|Vedit macro language}}==
Vedit allows using either '\' or '/' as directory separator character, it is automatically converted to the one used by the operating system.
<lang vedit>// In current directory
// In current directory
File_Rename("input.txt", "output.txt")
File_Rename("docs", "mydocs")
Line 400 ⟶ 381:
// In the root directory
File_Rename("/input.txt", "/output.txt")
File_Rename("/docs", "/mydocs")</lang>
</lang>
 
=={{header|Visual Basic .NET}}==
Line 407 ⟶ 387:
 
{{works with|Visual Basic .NET|9.0+}}
<lang vbnet> 'Current Directory
IO.Directory.Move("docs", "mydocs")
IO.File.Move("input.txt", "output.txt")
 
'Root
IO.Directory.Move("\docs", "\mydocs")
IO.File.Move("\input.txt", "\output.txt")
 
'Root, platform independent
IO.Directory.Move(IO.Path.DirectorySeparatorChar & "docs", _
IO.Path.DirectorySeparatorChar & "mydocs")
IO.File.Move(IO.Path.DirectorySeparatorChar & "input.txt", _
IO.Path.DirectorySeparatorChar & "output.txt")</lang>
 
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a filesystem, just namespaced variables. -->
Anonymous user