Created
August 12, 2009 09:57
-
-
Save charlesroper/166433 to your computer and use it in GitHub Desktop.
A script to fix filenames that are incompatible with Windows.
This file contains 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/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