Skip to content

Instantly share code, notes, and snippets.

@brianhempel
Created April 23, 2012 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianhempel/2471546 to your computer and use it in GitHub Desktop.
Save brianhempel/2471546 to your computer and use it in GitHub Desktop.
Script for mass renaming of files via Regexp
#!/usr/bin/env ruby
require 'fileutils'
if ARGV.size < 3
puts <<-USAGE
mass_rename [file1 file2 ...] <regexp> <replacement>
USAGE
end
file_names, regexp, replacement = ARGV[0...-2], ARGV[-2], ARGV[-1]
file_names.each do |old_name|
new_name = old_name.gsub(/#{regexp}/, replacement)
if old_name != new_name
puts "Renaming \"#{old_name}\"\tto \"#{new_name}\"..."
FileUtils.mv(old_name, new_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment