Created
December 16, 2009 14:07
-
-
Save oelmekki/257842 to your computer and use it in GitHub Desktop.
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/env ruby | |
require 'fileutils' | |
include FileUtils | |
def usage | |
puts <<EOS | |
usage: #{$0} glob-pattern from to | |
in all files matched by glob-pattern (classical shell | |
glob pattern), replace from (ruby regex) with to (ruby | |
substitution for gsub) | |
options: | |
--help | |
-h display this help | |
--pretend | |
-p show what replacements will be without | |
actually moving files | |
example: | |
mv trail-* 'trail-(.*)\.jpg' 'head-\\1.jpg' | |
EOS | |
end | |
files = Array.new | |
to = nil | |
from = nil | |
while ( arg = ARGV.pop ) | |
case arg | |
when '--help', '-h' | |
usage | |
exit | |
when '--pretend', '-p' | |
pretend = true | |
else | |
unless to | |
to = arg | |
else | |
unless from | |
from = Regexp.new arg | |
else | |
files << arg | |
end | |
end | |
end | |
end | |
unless from and to and files.length > 0 | |
usage | |
exit | |
end | |
files.each do |file| | |
new_name = file.gsub( from, to ) | |
puts "#{file} -> #{new_name}" | |
mv file, new_name unless defined?( pretend ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment