开发者

Erlang create directory

开发者 https://www.devze.com 2023-02-06 17:06 出处:网络
I need create subdirectory in specific directory. In erlang docs i find only file:make_dir/1 which create dir in the project source dir. How can i create directory in other dir?

I need create subdirectory in specific directory.

In erlang docs i find only file:make_dir/1 which create dir in the project source dir. How can i create directory in other dir?

I find solution. Maybe it will be interesting somebody:

filelib:ensure_dir("/this/path/will/soon/exist/").
开发者_StackOverflow

Thank you.


You can ensure that a directory exists (and create it if it doesn't, which is what you're looking for) using filelib:ensure_dir.

Example:

filelib:ensure_dir("/this/path/will/soon/exist/")

References:

  • filelib:ensure_dir documentation


The documentation must have been unclear as you can use file:make_dir/1 to create any directory which you would normally be allowed to create. It does not create all directories in the path, this you have to do explicitly yourself.

Using filelib:ensure_dir/1 with a path terminated by "/" is not documented but it is explicitly handled in the code so I doubt that it will go away.


This will create a subdirectory and confirm or create any parent directories needed

make_dir(Dir) ->
  % create or confirm the parent Dir exists (works recursively)
  ok = filelib:ensure_dir(Dir),
  % create or confirm Dir exists
  case file:make_dir(Dir) of
    ok -> ok;
    {error, eexist} -> ok;
    {error, Reason} -> {error, Reason}
  end.
0

精彩评论

暂无评论...
验证码 换一张
取 消