D: cross-platform way to go to the upper level of the path?

I have a path in a variable, for example:

D:\foo\bar\baz\file.txt

It could also be:

/foo/bar/baz/file.txt

I need a cross platform way to go to a directory bar.

I found only one way, but it looks like a hack:

writeln(mystr.replaceLast("baz" ~ dirSeparator ~ "file.txt", ""));

+4
source share
2 answers

std.pathhas 2 functions for this: buildNormalizedPathand asNormalizedPath.

The only difference between the two is that asNormalizedPaththey will not allocate memory.

+4
source

For some reason, dpaste using dmd 2.068.2 is not very happy. But this is the correct code.

import std.path;
import std.stdio;

void main() {
    version(Posix) {
        writeln(buildNormalizedPath("/a/b/c", "../d"));
    } else version(Windows) {
        writeln(buildNormalizedPath("c:\\\\a\\b\\c", "..\\d"));
    }
}
+2
source

Source: https://habr.com/ru/post/1612406/


All Articles