Skip to content

Instantly share code, notes, and snippets.

@sgallese
Created May 19, 2010 14:16
Show Gist options
  • Save sgallese/406345 to your computer and use it in GitHub Desktop.
Save sgallese/406345 to your computer and use it in GitHub Desktop.
# analyzefolder.rb
# goes through a folder in a project
# prints out useful infor pertaining to
# folder contents
# usage: ruby -w analyzefolder.rb
# The program will go through the following folder:
# @path_to_folder+@folder
# E.g.:
# /Users/claireharlam/Desktop/brown-digital-repository/Screen/
# where
# my @path_to_foldder is /Users/claireharlam/Desktop/brown-digital-repository/Screen/
# Set the path to the folder to where you store your projects
@path_to_folder = "/Users/claireharlam/Desktop/brown-digital-repository/text.curtain/"
class Mime
attr_accessor :type, :bytes, :files
end
def search_mime(term, bytes)
if @mimes.empty?
@mimes.push(create_mime(term, bytes))
else
found_mime = false
@mimes.each do | current |
if (current.type.eql?(term))
found_mime = true
current.bytes += bytes
current.files += 1
end
end
if ( !found_mime)
@mimes.push(create_mime(term, bytes))
end
end
end
def create_mime (term, bytes)
newMime = Mime.new
newMime.type = term
newMime.bytes = bytes * 1.0
newMime.files = 1.0
return newMime
end
def recurse_dir( current_dir )
@mimes = Array.new
if ( !(File::directory?( current_dir )) )
puts "Analyzing #{current_dir.chomp("/")}"
print `file -Ib "#{current_dir.chomp("/")}"`.chomp
print "\t"
print "1.0"
print "\t"
print `ls -s "#{current_dir.chomp("/")}"`.to_i * 2.0
print "\t"
print "100.0"
print "\t"
print "100.0"
print "\n"
else
Dir.foreach( current_dir ) do |entry|
# Ignore the following files:
# . : self directory
# .. : parent directory
# .DS_Store : Mac OSX folder attrributes
# Icon? : Max OS Classic folder attributes
if (entry == "." ||
entry == ".." ||
entry == ".DS_Store" ||
entry == entry.scan(/Icon./)[0] )
else
# File mimetype
term = `file -Ib "#{current_dir+entry}"`.chomp
# Convert size to kilobytes from 512 byte blocks
bytes = `ls -s "#{current_dir+entry}"`.to_i * 2
search_mime(term, bytes)
if ( File::directory?( current_dir + entry ) )
recurse_dir(current_dir + entry + "/")
end
end
end
puts "Analyzing #{@path_to_folder + @folder}"
@total_files = 0
@total_bytes = 0
@mimes.each do |current|
@total_files += current.files
@total_bytes += current.bytes
end
@mimes.each do |current|
print "#{current.type}"
print "\t"
print "#{current.files}"
print "\t"
print "#{current.bytes}"
print "\t"
print "#{current.files / @total_files * 100.0}"
print "\t"
print "#{current.bytes / @total_bytes * 100.0}"
print "\n"
end
end
end
Dir.foreach( @path_to_folder ) do |entry|
if (entry == "." ||
entry == ".." ||
entry == ".DS_Store" ||
entry == entry.scan(/Icon./)[0] )
else
@folder = entry + "/"
recurse_dir( @path_to_folder + @folder)
end
end
recurse_dir( @path_t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment