Skip to content

Instantly share code, notes, and snippets.

@MarkNenadov
Created October 24, 2023 11:59
Show Gist options
  • Save MarkNenadov/d9380a242e0e8257d98524815c738ccb to your computer and use it in GitHub Desktop.
Save MarkNenadov/d9380a242e0e8257d98524815c738ccb to your computer and use it in GitHub Desktop.
ruby recursive image tally
def image_file?(file, accepted_extensions)
accepted_extensions.include?(File.extname(file).downcase)
end
def recursive_image_tally(directory, image_counts)
image_count = 0
Dir.foreach(directory) do |file|
next if file.start_with?('.')
file_path = File.join(directory, file)
if File.directory?(file_path)
recursive_image_tally(file_path, image_counts)
elsif image_file?(file, image_counts.keys())
image_counts[File.extname(file).downcase] = image_counts[File.extname(file).downcase] + 1
end
end
end
image_counts = {'.jpg' => 0, '.jpeg' => 0, '.png' => 0, '.gif' => 0}
recursive_image_tally('.', image_counts)
puts "Image Type Counts: #{image_counts}"
puts "Total: #{image_counts.values.sum}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment