0

I want to find files of certain types under a directory. The extension part can have an arbitrary character case combination.

Path.wildcard("/...some path.../**/*.MPG")

The example above would only return files having uppercase .MPG extension, while I'd also like to get the downcase .mpg files along with whatever case combinations there could occur.

Is there a way to do that without listing all possible case combinations in the glob? I'd like this to work on Windows, Linux and OS X.

Nic Nilov
  • 5,056
  • 2
  • 22
  • 37

1 Answers1

1

You can use square brackets to express this in O(n) characters instead of listing out all combinations, which would be O(n!). For this case, you can do:

Path.wildcard("/...some path.../**/*.[mM][pP][gG]")

You can also create a function to do this automatically for you:

defmodule Main do
  def ci(<<char::utf8, rest::binary>>) do
    char = <<char::utf8>>
    "[#{String.downcase(char)}#{String.upcase(char)}]" <> ci(rest)
  end
  def ci(""), do: ""

  def main do
    IO.inspect "*.#{ci("mpg")}"
    Path.wildcard "*.#{ci("mpg")}"
  end
end

Main.main
$ ls | grep -i mpg
bar.mpG
baz.mpg
foo.MPG
$ elixir a.exs
"*.[mM][pP][gG]"
["bar.mpG", "baz.mpg", "foo.MPG"]
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • I tried the square brackets approach before and for some reason it didn't work for me. Now it does, maybe I did a mistake somewhere. Thanks! – Nic Nilov Nov 13 '16 at 12:01
  • @Dogbert, how do we handle cases when "some path" is "Some Path" or "Some path" or "SOME PATH"? https://stackoverflow.com/questions/45597018/how-to-perform-a-case-insensitive-file-search-in-erlang-elixir – Charles Okwuagwu Aug 09 '17 at 17:35