Path.rename

Rename current path.

Note: case of moving file/dir between filesystesm is not tested.

  1. void rename(Path to)
  2. void rename(string to)
    struct Path
    @safe const
    void
    rename
    (
    in string to
    )

Throws

PathException when destination already exists

Examples

1 import dshould;
2 Path root = createTempPath();
3 scope(exit) root.remove();
4 
5 // Create file
6 root.join("test-file.txt").exists.should.be(false);
7 root.join("test-file-new.txt").exists.should.be(false);
8 root.join("test-file.txt").writeFile("test");
9 root.join("test-file.txt").exists.should.be(true);
10 root.join("test-file-new.txt").exists.should.be(false);
11 
12 // Rename file
13 root.join("test-file.txt").exists.should.be(true);
14 root.join("test-file-new.txt").exists.should.be(false);
15 root.join("test-file.txt").rename(root.join("test-file-new.txt"));
16 root.join("test-file.txt").exists.should.be(false);
17 root.join("test-file-new.txt").exists.should.be(true);
18 
19 // Try to move file to existing directory
20 root.join("my-dir").mkdir;
21 root.join("test-file-new.txt").rename(root.join("my-dir")).should.throwA!PathException;
22 
23 // Try to rename one olready existing dir to another
24 root.join("other-dir").mkdir;
25 root.join("my-dir").exists.should.be(true);
26 root.join("other-dir").exists.should.be(true);
27 root.join("my-dir").rename(root.join("other-dir")).should.throwA!PathException;
28 
29 // Create test dir with contents
30 root.join("test-dir").mkdir();
31 root.join("test-dir", "f1.txt").writeFile("f1");
32 root.join("test-dir", "d2").mkdir();
33 root.join("test-dir", "d2", "f2.txt").writeFile("f2");
34 
35 // Ensure test dir with contents created
36 root.join("test-dir").exists.should.be(true);
37 root.join("test-dir").isDir.should.be(true);
38 root.join("test-dir", "f1.txt").exists.should.be(true);
39 root.join("test-dir", "f1.txt").isFile.should.be(true);
40 root.join("test-dir", "d2").exists.should.be(true);
41 root.join("test-dir", "d2").isDir.should.be(true);
42 root.join("test-dir", "d2", "f2.txt").exists.should.be(true);
43 root.join("test-dir", "d2", "f2.txt").isFile.should.be(true);
44 
45 // Try to rename directory
46 root.join("test-dir").rename(root.join("test-dir-new"));
47 
48 // Ensure old dir does not exists anymore
49 root.join("test-dir").exists.should.be(false);
50 root.join("test-dir", "f1.txt").exists.should.be(false);
51 root.join("test-dir", "d2").exists.should.be(false);
52 root.join("test-dir", "d2", "f2.txt").exists.should.be(false);
53 
54 // Ensure test dir was renamed successfully
55 root.join("test-dir-new").exists.should.be(true);
56 root.join("test-dir-new").isDir.should.be(true);
57 root.join("test-dir-new", "f1.txt").exists.should.be(true);
58 root.join("test-dir-new", "f1.txt").isFile.should.be(true);
59 root.join("test-dir-new", "d2").exists.should.be(true);
60 root.join("test-dir-new", "d2").isDir.should.be(true);
61 root.join("test-dir-new", "d2", "f2.txt").exists.should.be(true);
62 root.join("test-dir-new", "d2", "f2.txt").isFile.should.be(true);
63 
64 
65 version(Posix) {
66     // Prepare test dir in user's home directory
67     Path home_tmp = createTempPath("~", "tmp-d-test");
68     scope(exit) home_tmp.remove();
69 
70     // Ensure that there is no test dir in our home/based temp dir;
71     home_tmp.join("test-dir").exists.should.be(false);
72     home_tmp.join("test-dir", "f1.txt").exists.should.be(false);
73     home_tmp.join("test-dir", "d2").exists.should.be(false);
74     home_tmp.join("test-dir", "d2", "f2.txt").exists.should.be(false);
75 
76     root.join("test-dir-new").rename(
77             Path("~").join(home_tmp.baseName, "test-dir"));
78 
79     // Ensure test dir was renamed successfully
80     home_tmp.join("test-dir").exists.should.be(true);
81     home_tmp.join("test-dir").isDir.should.be(true);
82     home_tmp.join("test-dir", "f1.txt").exists.should.be(true);
83     home_tmp.join("test-dir", "f1.txt").isFile.should.be(true);
84     home_tmp.join("test-dir", "d2").exists.should.be(true);
85     home_tmp.join("test-dir", "d2").isDir.should.be(true);
86     home_tmp.join("test-dir", "d2", "f2.txt").exists.should.be(true);
87     home_tmp.join("test-dir", "d2", "f2.txt").isFile.should.be(true);
88 }

Meta