Skip to content

Instantly share code, notes, and snippets.

@anildigital
Created October 15, 2008 15:37
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 anildigital/16937 to your computer and use it in GitHub Desktop.
Save anildigital/16937 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Author: Anil Wadghule
#
# FRenamer: Renames image files for passed command line parameters
# Usage: frenamer . "the_dark_knight"
# It renames image files in current directory with names like the_dark_knight_1.jpg, the_dark_knight_2.jpg ...
#
# Installation:
# Copy it to your /usr/bin or any directory which is in your PATH
# chomod 777 it, to make it executable
#
#
Dir.chdir(ARGV[0])
cnt = 1
Dir["*"].each do |filename|
puts "renaming"
regex = /(?:.*)\.(png|jpg|gif)$/i
fname = ARGV[1] || "picture"
suffix = "#{fname}_#{cnt}"
new_filename = filename.gsub(regex, "#{suffix}.\\1")
puts new_filename
File.rename(filename, new_filename)
cnt += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment