Path.remove

Remove file or directory referenced by this path. This operation is recursive, so if path references to a direcotry, then directory itself and all content inside referenced dir will be removed

struct Path
@safe const
void
remove
()

Examples

1 import dshould;
2 Path root = createTempPath();
3 scope(exit) root.remove();
4 
5 // Try to remove unexisting file
6 root.join("unexising-file.txt").remove.should.throwA!(std.file.FileException);
7 
8 // Try to remove file
9 root.join("test-file.txt").exists.should.be(false);
10 root.join("test-file.txt").writeFile("test");
11 root.join("test-file.txt").exists.should.be(true);
12 root.join("test-file.txt").remove();
13 root.join("test-file.txt").exists.should.be(false);
14 
15 // Create test dir with contents
16 root.join("test-dir").mkdir();
17 root.join("test-dir", "f1.txt").writeFile("f1");
18 root.join("test-dir", "d2").mkdir();
19 root.join("test-dir", "d2", "f2.txt").writeFile("f2");
20 
21 // Ensure test dir with contents created
22 root.join("test-dir").exists.should.be(true);
23 root.join("test-dir").isDir.should.be(true);
24 root.join("test-dir", "f1.txt").exists.should.be(true);
25 root.join("test-dir", "f1.txt").isFile.should.be(true);
26 root.join("test-dir", "d2").exists.should.be(true);
27 root.join("test-dir", "d2").isDir.should.be(true);
28 root.join("test-dir", "d2", "f2.txt").exists.should.be(true);
29 root.join("test-dir", "d2", "f2.txt").isFile.should.be(true);
30 
31 // Remove test directory
32 root.join("test-dir").remove();
33 
34 // Ensure directory was removed
35 root.join("test-dir").exists.should.be(false);
36 root.join("test-dir", "f1.txt").exists.should.be(false);
37 root.join("test-dir", "d2").exists.should.be(false);
38 root.join("test-dir", "d2", "f2.txt").exists.should.be(false);
39 
40 
41 version(Posix) {
42     // Prepare test dir in user's home directory
43     Path home_tmp = createTempPath("~", "tmp-d-test");
44     scope(exit) home_tmp.remove();
45 
46     // Create test dir with contents
47     home_tmp.join("test-dir").mkdir();
48     home_tmp.join("test-dir", "f1.txt").writeFile("f1");
49     home_tmp.join("test-dir", "d2").mkdir();
50     home_tmp.join("test-dir", "d2", "f2.txt").writeFile("f2");
51 
52     // Remove created directory
53     Path("~").join(home_tmp.baseName).toAbsolute.toString.should.equal(home_tmp.toString);
54     Path("~").join(home_tmp.baseName, "test-dir").remove();
55 
56     // Ensure directory was removed
57     home_tmp.join("test-dir").exists.should.be(false);
58     home_tmp.join("test-dir", "f1.txt").exists.should.be(false);
59     home_tmp.join("test-dir", "d2").exists.should.be(false);
60     home_tmp.join("test-dir", "d2", "f2.txt").exists.should.be(false);
61 }

Meta