View Source Xfile
Xfile
is a utility module providing augmentations to the functions in the built-in File
module, including the support of recursive file listing, grep
and other programmatic filtering of filenames and lines, line counts (think wc -l
), and head
and tail
functions.
See the Xfile module for full API documentation.
examples
Examples
ls
ls
Use a regular expression to return only .txt
files:
iex> {:ok, stream} = Xfile.ls("path/to/files", filter: ~r/\.txt$/)
{:ok, #Function<59.58486609/2 in Stream.transform/3>}
iex> Enum.to_list(stream)
[
"path/to/files/a.txt",
"path/to/files/b.txt",
"path/to/files/subdir/c.txt"
]
Use a function to apply more complex logic to filter the results:
iex> {:ok, stream} = Xfile.ls("mydir", filter: fn x ->
stat = File.stat!(x)
stat.size > 1024
end)
{:ok, #Function<59.58486609/2 in Stream.transform/3>}
iex> Enum.to_list(stream)
[
"mydir/big-file",
"mydir/big-file2",
# ...
]
Limit the depth of the recursion to the given directory and its subdirectories, but no further:
iex> {:ok, stream} = Xfile.ls("top/dir", recursive: 1)
{:ok, #Function<59.58486609/2 in Stream.transform/3>}
iex> Enum.to_list(stream)
[
"top/dir/a",
"top/dir/b",
# ...
"top/dir/sub1/x",
"top/dir/sub1/y"
]
grep
grep
You can use a string, a regular expression, or an arity 1 function to find matching lines within the given file:
iex> Xfile.grep("dir", ".gitignore")
|> Enum.to_list()
["# The directory Mix will write compiled artifacts to.\\n",
"# The directory Mix downloads your dependencies sources to.\\n"]
See Xfile for more functions and examples.
installation
Installation
If available in Hex, the package can be installed
by adding xfile
to your list of dependencies in mix.exs
:
def deps do
[
{:xfile, "~> 0.4.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/xfile.
image-attribution
Image Attribution
Alien by Martin Smith from NounProject.com