File Path Handling in D

github logo Updated on ・1 min read

Toolbox (8 Part Series)

1) Parsing a String in D 2) Slicing and Dicing Arrays in D 3 ... 6 3) Execute a Program in D 4) File Path Handling in D 5) List Comprehension in D 6) Printing in D 7) Regular Expressions in D 8) Differences of Dates in D

Working with file paths is very annoying, especially on Windows where it supports both separators most of the time.

Aside from deciding which slash to use there are a couple of areas to consider.

  • Home directory
  • Relative vs absolute

D provides quick work of these.


import std.path;

However trying to build a path by adding slash separators is not the best choice for building a path from variables.

auto a = "foo";
auto b = "bar";

assert([a, b].joiner("/").equal(a.buildPath(b)));

And further work can be done on extensions.

assert("file.ext".extension == ".ext");
assert("file.ext".setExtension("gif") == "file.gif");
assert("file.ext".stripExtension == "file");
twitter logo DISCUSS
Classic DEV Post from Oct 11 '19

What was your win this week?

Got to all your meetings on time? Started a new project? Fixed a tricky bug?

Jesse Phillips profile image
Long time solo programmer. Building CI/CD and having the opportunity to improve collaboration.