#!/usr/bin/env ruby -Ku
# -*- encoding: utf-8 -*-
def main
batch_name, file_type = user_inputs
cnt = 0
Dir["*.#{file_type}"].each_with_index do |file, i|
new_name = "#{batch_name}#{i+1}.#{file_type}"
unless File.exist?(new_name)
File.rename(file, new_name)
cnt += 1
end
end
puts "#{cnt} files renamed."
rescue => e
puts e
end
def user_inputs
batch_name = ARGV[0] || (print "Input batch name: "; STDIN.gets.chomp)
file_type = ARGV[1] || (print "Input target file type: "; STDIN.gets.downcase.chomp)
puts "Filename: '#{batch_name}[number].#{file_type}' OK? (y|n)"
exit unless STDIN.gets =~ /^y/i
return batch_name, file_type
end
main
Tuesday, May 18, 2010
ruby:rename_file
rename_file.rb
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment