Created
February 15, 2013 19:36
-
-
Save rgm/4962820 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
# run with ARGV[0] pointed at an iPhone backup directory to re-tag the files | |
# with extensions. | |
# | |
# written under the influence of learning haskell / clojure / frp so also quite | |
# possibly the worst ruby ever written. | |
class String | |
def iphone_extension | |
types = { | |
/JPEG/i => 'jpg', | |
/PNG/i => 'png', | |
/Apple binary/i => 'plist', | |
/ASCII/i => 'txt', | |
/QuickTime/i => 'mov', | |
/SGML/i => 'html', | |
/HTML/i => 'html', | |
/data/i => 'raw', | |
/XML/i => 'xml', | |
/MPEG ADTS/ => 'mp3', | |
/vCard/i => 'vcard', | |
/iTunes AAC/i => 'm4a', | |
/PDF/ => 'pdf', | |
/CoreAudio/ => 'caf', | |
} | |
@iphone_file_type ||= types.keys.select {|t| self =~ t}.map {|match| types[match]}.first | |
end | |
def detect_file_type | |
`file #{self}`.strip.split(": ") | |
end | |
end | |
class Array | |
def rename_using_type | |
`mv #{self[0]}{,.#{self[1].iphone_extension}}` if self[1].iphone_extension | |
end | |
end | |
Dir[File.join(ARGV[0],'*')] | |
.map(&:detect_file_type) | |
.each(&:rename_using_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment