rename

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

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.

  1. void rename(RF from, RT to)
  2. void rename(RF from, RT to)
    void
    rename
    (
    RF
    RT
    )
    (
    auto ref RF from
    ,
    auto ref RT to
    )

Parameters

from RF

string or range of characters representing the existing file name

to RT

string or range of characters representing the target file name

Throws

FileException on error.

Examples

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

t1.write("1");
t1.rename(t2);
assert(t2.readText == "1");

t1.write("2");
t1.rename(t2);
assert(t2.readText == "2");

Meta