How to retrieve zip files from one folder and extract their contents to another folder?

Hello,

I am new to Elixir programming.
I’m attempting to retrieve zip files from one folder and extract their contents to another folder.
Although the code compiles without issues, I encounter the following error when executing it:

Failed to extract test1.zip because of {:EXIT, {{:badmatch, “test1.zip” …

Here is a part of the code :

for zip_file <- File.ls!(".") |> Enum.filter(&String.ends_with?(&1, ".zip")) do
       case :zip.unzip(zip_file, [{:cwd, "."}]) do
        {:ok, extracted_files} ->
          # Filter for files with ".dcm" extension
          dcm_files = Enum.filter(extracted_files, &String.ends_with?(&1, ".dcm"))
	end
end

Thank you for your help

For starters, I’d add an error clause to the case so I get to see the error.

1 Like

Hello and welcome

You could use…

Path.wildcard "*.zip"

Commands that start with :atom are generally from Erlang.

Erlang uses charlists while Elixir uses binaries… that should work

:zip.unzip(to_charlist(zip_file), [{:cwd, "."}])

# Or the Elixir way...

zip_file
|> to_charlist()
|> :zip.unzip([{:cwd, "."}])
1 Like

Thank you very much for your help.
I appreciate.