Browse | Submit A New Snippet | Create A Package

 

Filenames to 7-bit ASCII

Type:
Full Script
Category:
File Management
License:
Ruby License
Language:
Ruby
 
Description:
Convert all directories and files under a given path to contain only 7-bit ascii.
Transliteration is done trough a easy to expand lookup table.
To commit things, replace FileUtils::DryRun with FileUtils

Versions Of This Snippet::

Kaspar Schiess
Snippet ID Download Version Date Posted Author Delete
771.02005-02-09 09:57Kaspar Schiess

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 version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..