|
Versions Of This Snippet::
Download a raw-text version of this code by clicking on "Download Version"
Latest Snippet Version: :1.0
# Converts Umlauts.
require 'find'
require 'logger'
require 'fileutils'
raise 'Expects target path to start from as argument' if ARGV.size!=1
logger = Logger.new( 'rename.log' )
REPLACEMENTS = %w{
ä ae ö oe ü ue à A
© (c) é e à a ú u
í i á a ñ n ç c ´ '
À A š s Ä Ae ß ss
ù u è e ï i ê e ó o
ÿ y Ö Oe â a ˜ +
Ü Ue Ç C ë e ô o
_
}
rephash = Hash[*REPLACEMENTS]
re_specials = Regexp.new("(["+Regexp.escape(rephash.keys.join)+"])")
include FileUtils::DryRun
directories = []
Find.find(ARGV[0]) do |path|
if FileTest.directory? path
directories << path
else
name = File.basename( path )
full_path = File.dirname( path )
problematic = name.unpack('C*').detect { |code| code>127 }
if problematic
new_name = name.gsub( re_specials ) do |match|
rephash[$1]
end
logger.info "[#{full_path}], [#{name}], [#{new_name}]"
mv( File.join(full_path, name), File.join(full_path, new_name) )
end
end
end
directories.reverse.each do |dir|
name = File.basename(dir)
full_path = File.dirname(dir)
problematic = name.unpack('C*').detect { |code| code>127 }
if problematic
new_name = name.gsub( re_specials ) do |match|
rephash[$1]
end
logger.info "[#{full_path}], [#{name}], [#{new_name}]"
mv( File.join(full_path, name), File.join(full_path, new_name) )
end
end
Submit a new versionYou can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..
|
||||||||||||||||||||||||||
