Directories in D Programming Language

Here’s the translation of the Go code to D, formatted in Markdown suitable for Hugo:

import std.stdio;
import std.file;
import std.path;
import std.algorithm;

void check(string msg = "")
{
    if (msg != "")
    {
        throw new Exception(msg);
    }
}

void main()
{
    // Create a new sub-directory in the current working
    // directory.
    mkdir("subdir", 0755);

    // When creating temporary directories, it's good
    // practice to use scope(exit) for their removal.
    // std.file.rmdirRecurse will delete a whole directory tree
    // (similarly to `rm -rf`).
    scope(exit) rmdirRecurse("subdir");

    // Helper function to create a new empty file.
    void createEmptyFile(string name)
    {
        std.file.write(name, "");
    }

    createEmptyFile("subdir/file1");

    // We can create a hierarchy of directories, including
    // parents with mkdirRecurse. This is similar to the
    // command-line `mkdir -p`.
    mkdirRecurse("subdir/parent/child", 0755);

    createEmptyFile("subdir/parent/file2");
    createEmptyFile("subdir/parent/file3");
    createEmptyFile("subdir/parent/child/file4");

    // dirEntries lists directory contents, returning an
    // InputRange of DirEntry objects.
    writeln("Listing subdir/parent");
    foreach (entry; dirEntries("subdir/parent", SpanMode.shallow))
    {
        writeln(" ", entry.name, " ", entry.isDir);
    }

    // chdir lets us change the current working directory,
    // similarly to `cd`.
    chdir("subdir/parent/child");

    // Now we'll see the contents of `subdir/parent/child`
    // when listing the *current* directory.
    writeln("Listing subdir/parent/child");
    foreach (entry; dirEntries(".", SpanMode.shallow))
    {
        writeln(" ", entry.name, " ", entry.isDir);
    }

    // cd back to where we started.
    chdir("../../..");

    // We can also visit a directory *recursively*,
    // including all its sub-directories. walkDir accepts
    // a callback function to handle every file or
    // directory visited.
    writeln("Visiting subdir");
    walkDir("subdir", &visit);
}

// visit is called for every file or directory found
// recursively by walkDir.
void visit(string path)
{
    writeln(" ", path, " ", isDir(path));
}

This D code demonstrates working with directories, including creating directories, listing their contents, changing the current working directory, and recursively walking through a directory structure.

To run the program, save it as directories.d and use the D compiler:

$ dmd -run directories.d
Listing subdir/parent
 subdir/parent/child true
 subdir/parent/file2 false
 subdir/parent/file3 false
Listing subdir/parent/child
 subdir/parent/child/file4 false
Visiting subdir
 subdir true
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This example showcases D’s file system manipulation capabilities, which are similar to those in other systems programming languages. The std.file and std.path modules provide the necessary functions for directory operations, while std.algorithm is used for the walkDir function.