Created
October 23, 2011 08:18
-
-
Save miau/1307063 to your computer and use it in GitHub Desktop.
convert htwiki file names from URL-encoded EUC-JP to Shift_JIS
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| # ひとり Wiki ファイルのリネームスクリプト。 | |
| # | |
| # ひとり Wiki のファイルは EUC-JP での 16 進表現になっているので、 | |
| # これを Shift_JIS に変換してリネームしつつ、必要なディレクトリを | |
| # 作っていく。 | |
| # | |
| # 例: | |
| # BEF0CAF3B8BB2F554E4958B4D8CFA2.txt | |
| # ↓ | |
| # 情報源/Perl関連.txt | |
| require 'fileutils' | |
| files = Dir.glob("**/*.txt") | |
| files.each do |file| | |
| # ファイル名の分解 | |
| puts file | |
| pathname, filename = File.split(file) | |
| basename = File.basename(filename, ".txt") | |
| # EUC-JP -> Shift_JIS 変換 | |
| new_basename = [basename].pack('H*').encode('Shift_JIS', 'EUC-JP') | |
| # ファイル名として使えない文字を全角に変換 | |
| # ※ただし \ と / はディレクトリとして扱うので除外 | |
| new_basename.tr!(':*?"<>|'.encode('Shift_JIS'), ':*?”<>|'.encode('Shift_JIS')) | |
| # 変換後のディレクトリ名、ファイル名の組み立て | |
| new_pathname = pathname + "/" + File.dirname(new_basename) | |
| new_file = pathname + "/" + new_basename + ".txt" | |
| # 親ディレクトリの作成&リネーム | |
| puts " -> #{new_file}" | |
| FileUtils.mkdir_p(new_pathname) | |
| File.rename(file, new_file) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment