Created
October 24, 2023 11:59
-
-
Save MarkNenadov/d9380a242e0e8257d98524815c738ccb to your computer and use it in GitHub Desktop.
ruby recursive image tally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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