Reputation: 1150
I'm trying to scan a given directory and need to receive only directories back. This way returns all files and folders in a given dir
:
dir = "/Users/user/releases/"
folders =
File.ls(dir)
|> elem(1)
Is it possible to filter folders
to keep only directories, and no individual files? Thanks!
Upvotes: 2
Views: 1564
Reputation: 222050
You can filter the list of files/directories using File.dir?/1
to get only directories back. Since File.ls!/1
returns just the file name, and not the full path, you'll also need to join dir
with the file name before passing it to File.dir?/1
:
iex(1)> dir = "."
"."
iex(2)> File.ls!(dir) |> Enum.filter(&File.dir?(Path.join(dir, &1)))
["config", "lib", "test"]
iex(3)> dir = "test"
"test"
iex(4)> File.ls!(dir) |> Enum.filter(&File.dir?(Path.join(dir, &1)))
["foo"]
Upvotes: 6