6 # Renames all the files in a directory with the given file extension.
7 # The renamed files will just have numbers.
22 opts.starting_number = 1
23 $fu_args = { :verbose => true }
24 opts.preserve_names = false
26 # Fill in $opts values
27 parser = OptionParser.new do |myparser|
28 myparser.banner = "Usage: #{ File.basename($0) } [opts]"
29 myparser.separator("Specific options:")
30 myparser.on("--num-digits DIGITS", "-D",
31 "Set the number of digits in the numbering scheme.") do |d|
32 opts.num_digits = d.to_i
34 myparser.on("--dry-run", "-d",
35 "Show what would be done, without doing it.") do |d|
36 $fu_args = { :verbose => true, :noop => true }
39 myparser.on("--file-extension EXTENSION", "-e",
40 "The file extension for the files to rename.") do |e|
43 myparser.on("--preserve-names", "-p",
44 "Preserve the names while changing the numbers.") do |e|
45 opts.preserve_names = true
47 myparser.on("--starting-number NUMBER", "-N",
48 "The starting number (defaults to 1)") do |e|
49 opts.starting_number = e.to_i
51 myparser.on("--glob GLOB", "-g",
52 "Specify the glob expression to use. Example: '*/*.mp3'.") do |d|
58 raise "invalid num_digits: #{opts.num_digits}" unless
60 raise "must specify an extension" unless opts.extension != nil
61 if (opts.glob == nil) then
62 opts.glob = "*.#{$opts.extension}"
76 Dir.glob($opts.glob).sort.each do |f|
82 $total_files = $total_files + 1
85 def get_file_name(num)
86 return sprintf("%0#{$opts.num_digits}d", num)
89 def rename_files(file)
90 dst="#{get_file_name($opts.starting_number + $total_files)}.#{$opts.extension}"
92 FileUtils.mv(file, dst, $fu_args)
94 $total_files = $total_files + 1
97 def rename_files_keep_names(file)
99 if (file =~ /^[0-9. -]*(.*)$/) then
102 raise "can't find proper name for #{file}"
104 full_name = "#{get_file_name(1 + $total_files)} - #{proper_file}"
105 FileUtils.mv(file, full_name, $fu_args)
106 $total_files = $total_files + 1
111 $opts = MyOptions.parse(ARGV)
112 rescue Exception => msg
113 $stderr.print("#{msg}.\nType --help to see usage information.\n")
117 # make sure there aren't too many files
119 max_total_files = pow(10, $opts.num_digits) - 1
120 file_iter { |f| count_files(f) }
121 if ($total_files > max_total_files) then
122 raise "With #{$opts.num_digits} digit(s), we can only have at most \
123 #{max_total_files} files-- but there are #{$total_files} files in the \
124 #directory. Try setting a higher value for num_digits, using -D."
129 if ( $opts.preserve_names ) then
130 file_iter { |f| rename_files_keep_names(f) }
132 file_iter { |f| rename_files(f) }