Skip to content

Instantly share code, notes, and snippets.

@radamanthus
Created March 29, 2013 08:27
Show Gist options
  • Save radamanthus/5269469 to your computer and use it in GitHub Desktop.
Save radamanthus/5269469 to your computer and use it in GitHub Desktop.
Rename a batch of files that are following a sequential naming conventions, for example, a series of image files with names 9471_01_01.png..9471_01_29.png. Use this gist if you need to insert a file into the series. In this example, if you need to insert an 18th file, you will have to rename 9471_01_18.png to 9471_01_19.png, 19.png to 20.png, an…
file_prefix = ARGV[0]
file_suffix = ARGV[1]
start_file_index = ARGV[2].to_i
end_file_index = ARGV[3].to_i
file_ids = (start_file_index..end_file_index).to_a.reverse
file_ids.each do |i|
current_file_id = "%02d" % i
new_file_id = "%02d" % (i+1)
puts "Renaming #{file_prefix}#{current_file_id}#{file_suffix} to #{file_prefix}#{new_file_id}#{file_suffix}"
File.rename("#{file_prefix}#{current_file_id}#{file_suffix}", "#{file_prefix}#{new_file_id}#{file_suffix}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment