View source code
Display the source code in std/file.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.file.rename

Rename file from to to, moving it between directories if required. If the target file exists, it is overwritten.

void rename(RF, RT) (
  RF from,
  RT to
)
if ((isInputRange!RF && !isInfinite!RF && isSomeChar!(ElementEncodingType!RF) || isSomeString!RF) && !isConvertibleToString!RF && (isInputRange!RT && !isInfinite!RT && isSomeChar!(ElementEncodingType!RT) || isSomeString!RT) && !isConvertibleToString!RT);

void rename(RF, RT) (
  auto ref RF from,
  auto ref RT to
)
if (isConvertibleToString!RF || isConvertibleToString!RT);

It is not possible to rename a file across different mount points or drives. On POSIX, the operation is atomic. That means, if to already exists there will be no time period during the operation where to is missing. See manpage for rename for more details.

Parameters

NameDescription
from string or range of characters representing the existing file name
to string or range of characters representing the target file name

Throws

FileException on error.

Example

auto t1 = deleteme, t2 = deleteme~"2";
scope(exit) foreach (t; [t1, t2]) if (t.exists) t.remove();

t1.write("1");
t1.rename(t2);
writeln(t2.readText); // "1"

t1.write("2");
t1.rename(t2);
writeln(t2.readText); // "2"

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis

License

Boost License 1.0.