Last active
August 29, 2015 14:05
-
-
Save quanon/d3c3118ffc75650ea2e2 to your computer and use it in GitHub Desktop.
ファイルをリネームするスクリプト
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
| # Usage | |
| # ruby rename.rb ~/Downloads/reg_test "homu_(\d+)_homu.(txt)" "sayaka_@[email protected]" | |
| require "fileutils" | |
| dir = File.expand_path(ARGV[0]) | |
| pattern = /#{ARGV[1]}$/ | |
| after = ARGV[2] | |
| Dir.glob(File.join(dir, "**/*")).each do |filename| | |
| match_data = pattern.match(filename).to_a.tap(&:shift) | |
| next if match_data.empty? | |
| marks = after.scan(/(@\d+)/).flatten | |
| rename_rule = Hash[marks.zip(match_data)] | |
| renamed = after | |
| rename_rule.each do |mark, str| | |
| renamed = renamed.gsub(mark, str) | |
| end | |
| renamed = File.join(File.dirname(filename), renamed) | |
| FileUtils.mv(filename, renamed) | |
| puts "#{filename} を #{renamed} にリネームしました。" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment