Skip to content

Instantly share code, notes, and snippets.

@angelo
Created January 20, 2011 00:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save angelo/787140 to your computer and use it in GitHub Desktop.
Save angelo/787140 to your computer and use it in GitHub Desktop.
This ruby script finds unused image assets in a website project.
#!/usr/bin/env ruby
require 'fileutils'
files = Dir['**/*.{htm,html,shtml,php,css,js}']
images = Dir['**/*.{jpg,png,gif,bmp}']
puts "#{images.size} images found & #{files.size} files found to search against"
content = ""
files.each do |filename|
content << File.read(filename)
end
images.each do |filename|
basename = File.basename filename
dirname = File.dirname filename
image_not_used = content !~ /\b#{basename}\b/
if image_not_used
FileUtils.mkdir_p "../unused/#{dirname}"
FileUtils.mv filename, "../unused/#{filename}"
puts "Image '#{filename}' moved to '../unused' folder"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment