Skip to content

Instantly share code, notes, and snippets.

@charlesroper
Created August 12, 2009 09:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesroper/166433 to your computer and use it in GitHub Desktop.
Save charlesroper/166433 to your computer and use it in GitHub Desktop.
A script to fix filenames that are incompatible with Windows.
#!/usr/bin/ruby
# tmsanitize4e.rb
# A script to sanitize filenames that are incompatible with Windows.
require 'uri'
require 'find'
require 'fileutils'
include FileUtils::Verbose
BADCHARS = /([<>\|:\*…\"\?\\“”↵—↓↵‘’])/
target_dir = ARGV[0]
def sanitize(f)
URI.escape(f, BADCHARS).chomp
end
def verbose_rename (orig_name, new_name)
File.rename(orig_name, new_name)
puts orig_name + " --> " + new_name
end
Find.find(target_dir) do |f|
orig_name = File.basename(f)
clean_name = sanitize(File.basename(f))
dirname = File.dirname(f) + "/"
verbose_rename(dirname + orig_name, dirname + clean_name) unless (orig_name === clean_name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment