Directories in Erlang

Here’s the translation of the Go code to Erlang, along with explanations in Markdown format suitable for Hugo:

Our example demonstrates working with directories in Erlang. Here’s the full source code:

-module(directories).
-export([main/0]).

-define(SUBDIR, "subdir").

main() ->
    % Create a new sub-directory in the current working directory
    ok = file:make_dir(?SUBDIR),

    % Helper function to create a new empty file
    CreateEmptyFile = fun(Name) ->
        ok = file:write_file(Name, <<>>)
    end,

    CreateEmptyFile(?SUBDIR ++ "/file1"),

    % Create a hierarchy of directories, including parents
    ok = filelib:ensure_dir(?SUBDIR ++ "/parent/child/"),
    ok = file:make_dir(?SUBDIR ++ "/parent/child"),

    CreateEmptyFile(?SUBDIR ++ "/parent/file2"),
    CreateEmptyFile(?SUBDIR ++ "/parent/file3"),
    CreateEmptyFile(?SUBDIR ++ "/parent/child/file4"),

    % List directory contents
    {ok, Files} = file:list_dir(?SUBDIR ++ "/parent"),
    io:format("Listing ~s/parent~n", [?SUBDIR]),
    lists:foreach(fun(File) ->
        FullPath = ?SUBDIR ++ "/parent/" ++ File,
        IsDir = filelib:is_dir(FullPath),
        io:format("  ~s ~p~n", [File, IsDir])
    end, Files),

    % Change the current working directory
    ok = file:set_cwd(?SUBDIR ++ "/parent/child"),

    % List contents of the current directory
    {ok, Files2} = file:list_dir("."),
    io:format("Listing ~s/parent/child~n", [?SUBDIR]),
    lists:foreach(fun(File) ->
        IsDir = filelib:is_dir(File),
        io:format("  ~s ~p~n", [File, IsDir])
    end, Files2),

    % Change back to the original directory
    ok = file:set_cwd("../../.."),

    % Recursively visit a directory
    io:format("Visiting ~s~n", [?SUBDIR]),
    filelib:fold_files(?SUBDIR, ".*", true, fun(F, _) ->
        IsDir = filelib:is_dir(F),
        io:format("  ~s ~p~n", [F, IsDir]),
        ok
    end, []),

    % Clean up
    os:cmd("rm -rf " ++ ?SUBDIR).

Let’s break down the key parts:

  1. We use file:make_dir/1 to create a new directory.

  2. A helper function CreateEmptyFile is defined to create empty files.

  3. filelib:ensure_dir/1 is used to create a hierarchy of directories, similar to mkdir -p in Unix.

  4. file:list_dir/1 is used to list directory contents.

  5. file:set_cwd/1 changes the current working directory, similar to cd in Unix.

  6. filelib:fold_files/5 is used to recursively visit a directory, applying a function to each file or directory.

  7. We use os:cmd/1 to remove the created directory tree at the end.

To run this program:

$ erlc directories.erl
$ erl -noshell -s directories main -s init stop
Listing subdir/parent
  child true
  file2 false
  file3 false
Listing 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 demonstrates how to work with directories in Erlang, including creating directories, listing contents, changing the working directory, and recursively visiting a directory tree.