How may we do case-insensitive file-searches in Erlang/Elixir?

Is there a way to do case-insensitive file-searches in Elixir/Erlang?

Matching is case-sensitive, for example, “a” does not match “A”. (http://erlang.org/doc/man/filelib.html#wildcard-1)

does not handle this completely.

You could always alternate the characters, so if searching for "blah.txt" and you want it to find any file regardless of case that matches then you could instead search for "[bB][lL][aA][hH].[tT][xX][tT]" or so, which is easily generateable.

Or you could just get all the files in the path and Enum.filter them out directly with a case insensitive regex or so.

/me thinks case-insensitive filenames is Not a good thing regardless

What happens when you are searching user created paths?

"DFR 200" or "Dfr 200" or "dfr 200"

iex(11)> fp = root <> "\\**DFR 200**\\20170101.{xls,xlsx}"
"C:\\Clients\\Pensure\\RECENT RMAS\\source files\\**DFR 200**\\20170101.{xls,xlsx}"
iex(12)> Path.wildcard fp                                 
["c:/Clients/Pensure/RECENT RMAS/source files/DFR 200/20170101.xlsx"]
iex(13)> fp = root <> "\\**dfr 200**\\20170101.{xls,xlsx}"
"C:\\Clients\\Pensure\\RECENT RMAS\\source files\\**dfr 200**\\20170101.{xls,xlsx}"
iex(14)> Path.wildcard fp                                 
[]

Not sure what you are asking? Also be careful about passing anything to the filesystem, even as a path, from user input, sanitize your inputs.

Got a very nice solution here:

defmodule A do
  def case_insensitive_glob(glob) do
    Regex.replace(~r/[a-zA-Z]/, glob, fn letter ->
      "[#{String.downcase(letter)}#{String.upcase(letter)}]"
    end)
  end
end

glob = A.case_insensitive_glob("**/*reAdmE.*") |> IO.inspect
Path.wildcard(glob) |> IO.inspect

Yep, that is precisely the kind of helper that would work then. :slight_smile:

This could be useful to have:

Path.wildcard("my path", [:case_insensitive])

I’m unsure how useful that would be (I’ve never needed a case insensitive path search in 30 years, seems very odd…), but you could always submit a PR to Elixir and see if it gets in. :slight_smile:

1 Like

mainly on windows