Skip to content

Instantly share code, notes, and snippets.

@katoy
Last active October 6, 2015 00:38
Show Gist options
  • Save katoy/2905143 to your computer and use it in GitHub Desktop.
Save katoy/2905143 to your computer and use it in GitHub Desktop.
Sample: Copy directory-tree with convert encoding to UTF8.
# 再帰的に Directory 階層を copy していく。
# ただし、 copy するのは *.uws, *.UWS だけを対象とし、utf-8 に encoding 変換もする。
#
# using ruby 1.8.7
# 2012-06-10 katoy
require 'rubygems'
require 'fileutils'
require 'pp'
require 'kconv'
KCODE="utf8"
class Utils
def self.copy_r(src, dest, &proc)
Dir.entries(src).each do |fname|
next if ("." === fname) or (".." === fname)
s = File.join(src, fname)
d = File.join(dest, fname)
if FileTest.directory?(s)
FileUtils.mkdir_p(d)
copy_r(s, d, &proc)
else
# copy_file(s, d)
proc.call(s, d) if block_given?
end
end
end
end
def copy_file(s, d)
pp(s + " -> " + d)
end
if __FILE__ == $0
def usage()
puts "usage: #{$0} src dest"
end
def is_target?(name)
name.match(/.*\.rb$/) # (/.*\.uws$/)
end
if ARGV.size < 2
usage()
exit(-1)
end
Utils.copy_r(ARGV[0], ARGV[1]) { |s, d|
if is_target?(s)
open(s) {|source|
open(d, "w") {|dest|
dest.write(source.read.toutf8)
}
}
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment