I’d probably do this:
def sort_files(path) do
files_by_dir =
File.ls!(path)
|> Enum.reject(&File.dir?/1)
|> Enum.group_by(&Path.extname/1)
Enum.each(files_by_dir, fn {"." <> ext, files} ->
extpath = Path.join(path, ext)
File.mkdir_p(extpath)
Enum.each(files, fn file ->
filepath = Path.join(extpath, file)
# Keep only a max of 9, could easily make this unbounded though, but eh useful feature to add
Enum.each(9..2, &File.rename("#{filepath}_#{&1-1}", "#{filepath}_#{&1}"))
File.rename(filepath, "#{filepath}_1")
File.rename!(Path.join(path, file), filepath)
end)
end)
end
Could use some error reporting, but eh.






















