FelixFortis
FelixFortis

Reputation: 724

How to return a collection of files from a given directory in Elixir?

In Ruby, I can use

target_files = Dir["/some/dir/path/*.rb"]
#=> ["/some/dir/path/foo.rb", "/some/dir/path/bar.rb", "/some/dir/path/baz.rb"]

which will return an array of all of the matching files in a directory. How can I do something similar in Elixir?

Upvotes: 24

Views: 11095

Answers (3)

hipertracker
hipertracker

Reputation: 2458

Path.wildcard/1 can handle recursive folders with two stars '**' (it works similar to Dir.glob in Ruby). E.g.

Path.wildcard("../data/??/**/*.yml")

Upvotes: 1

Dogbert
Dogbert

Reputation: 222060

You're looking for Path.wildcard/2:

iex(1)> Path.wildcard("/tmp/some/dir/path/*.rb")
["/tmp/some/dir/path/bar.rb", "/tmp/some/dir/path/baz.rb",
 "/tmp/some/dir/path/foo.rb"]
iex(2)> Path.wildcard("/tmp/**/*b*.rb")
["/tmp/some/dir/path/bar.rb", "/tmp/some/dir/path/baz.rb"]

Upvotes: 38

Michael Terry
Michael Terry

Reputation: 992

And if you want to recursively gather files with a regex, there's :filelib.fold_files/5.

Upvotes: 6

Related Questions