Copying and moving directories

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 17 02:16:44 PST 2017


On Friday, February 17, 2017 08:48:03 Kagamin via Digitalmars-d-learn wrote:
> On Thursday, 16 February 2017 at 17:06:30 UTC, Jonathan M Davis
>
> wrote:
> > Well, there's zero difference between renaming the file or
> > directory and moving it. It's simply a difference in name.
>
> Isn't there a difference?
> I though
> move("/path/dir1","dir2") moves folder to current directory and
> changes its name to dir2
> rename("/path/dir1","dir2") leaves folder where it was and only
> changes its name to dir2

No, rename does change the whole path, just like mv would. If you want to
just change the last part of the path, then you need to supply the entire
path again but just change the last part. e.g. this passes:

import std.file;

void main()
{
    scope(exit) if("a".exists) rmdirRecurse("a");
    scope(exit) if("c".exists) rmdirRecurse("c");

    mkdirRecurse("a/b");
    assert("a".exists);
    assert(!"c".exists);
    assert("a/b".exists);
    assert(!"a/c".exists);

    rename("a/b", "c");
    assert("a".exists);
    assert("c".exists);
    assert(!"a/b".exists);
    assert(!"a/c".exists);
}

Maybe there's another language that declares a move function and a rename
function like you describe, but POSIX has rename for C and mv for shell, and
both of them change the whole path, and on POSIX systems, D has just wrapped
the POSIX rename function, so its behavior is the same.

http://www.unix.com/man-page/FreeBSD/2/rename/
https://linux.die.net/man/2/rename

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list