-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrenameall
executable file
·42 lines (36 loc) · 1.25 KB
/
renameall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env ruby
# Start Date: Monday May 26, 2008
# Current Version: 0.9
# Author: Joseph Pecoraro
# Contact: [email protected]
# Decription: Quick and easy way to change the extension of all the
# files in a directory. Ideas for improvements are welcome.
# Wrapper around the functionality
class Renamer
attr_accessor :from_extension, :to_extension
# Quickly set the from and to extension
def initialize(from_extension, to_extension)
@from_extension, @to_extension = from_extension, to_extension
end
# Rename all the files in a given directory
# Default to the current directory
def rename_all_in_dir(dir="./")
Dir.glob(dir + "*.#{@from_extension}").each do |f|
changeTo = File.basename(f,@from_extension) + @to_extension
if File.exists? changeTo
puts "Warning: #{f.sub(dir,'')}: Did not change because #{changeTo} already existed!"
else
File.rename(f, changeTo)
end
end
end
end
# When run as as script
if $0 == __FILE__
if ARGV.size != 2
program_name = $0.split(/\//).last
puts "usage: #{program_name} from_extension to_extension\nExample: #{program_name} rhtml haml - changes the extension of all .rhtml files to .haml"
else
Renamer.new(ARGV[0], ARGV[1]).rename_all_in_dir
end
end