Created
September 28, 2008 22:22
-
-
Save kakra/13512 to your computer and use it in GitHub Desktop.
Useful if your mp3 player doesn't like fancy long filenames
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 | |
# | |
# Useful if your mp3 player doesn't like fancy long filenames | |
# | |
require 'digest/sha1' | |
Dir.glob("*.mp3") do |f| | |
name = f.downcase.split("."); | |
next if name.length < 2 | |
ext = name.pop | |
name = name.join.gsub(/(^|[-'()_ ]+)(\S|$)/) do | |
s = $2.upcase | |
if $1 =~ /^-/ | |
s = "-#{s}" | |
else | |
s | |
end | |
end | |
name = name[0...27] + "~" + Digest::SHA1.hexdigest(name)[0..3] if name.length > 32 | |
begin | |
File::rename(f, "#{name}.#{ext}") and puts "'#{f}' successfully renamed" | |
rescue SystemCallError => e | |
puts "Error renaming '#{f}'" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment