Makefile: add pickrand
[cmccabe-bin] / names_to_numbers.rb
1 #!/usr/bin/ruby -w
2
3 #
4 # names_to_numbers.rb
5 #
6 # Renames all the files in a directory with the given file extension.
7 # The renamed files will just have numbers.
8 #
9 # Colin Mccabe
10 #
11
12 require 'fileutils'
13 require 'optparse'
14 require 'ostruct'
15
16 class MyOptions
17   def self.parse(args)
18     opts = OpenStruct.new
19     opts.dry_run = false
20     opts.num_digits = 2
21     opts.extension = nil
22     opts.starting_number = 1
23     $fu_args = { :verbose => true }
24     opts.preserve_names = false
25
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
33       end
34       myparser.on("--dry-run", "-d",
35               "Show what would be done, without doing it.") do |d|
36         $fu_args = { :verbose => true, :noop => true }
37         opts.dry_run = true
38       end
39       myparser.on("--file-extension EXTENSION", "-e",
40               "The file extension for the files to rename.") do |e|
41         opts.extension = e
42       end
43       myparser.on("--preserve-names", "-p",
44               "Preserve the names while changing the numbers.") do |e|
45         opts.preserve_names = true
46       end
47       myparser.on("--starting-number NUMBER", "-N",
48               "The starting number (defaults to 1)") do |e|
49         opts.starting_number = e.to_i
50       end
51     end
52
53     parser.parse!(args)
54     raise "invalid num_digits: #{opts.num_digits}" unless
55       opts.num_digits > 0
56     raise "must give an extension" unless opts.extension != nil
57     return opts
58   end
59 end
60
61 def pow(x, y)
62   ret = 1
63   (0...y).each do |z|
64     ret = ret * x
65   end
66   return ret
67 end
68 #.#{$opts.extension}").sort.each do |f|
69 def file_iter
70   Dir.glob("*.#{$opts.extension}").sort.each do |f| 
71     yield f
72   end
73 end
74
75 def count_files(file)
76   $total_files = $total_files + 1
77 end
78
79 def get_file_name(num)
80   return sprintf("%0#{$opts.num_digits}d", num)
81 end
82
83 def rename_files(file)
84   dst="#{get_file_name($opts.starting_number + $total_files)}.#{$opts.extension}"
85   if (file != dst) then
86     FileUtils.mv(file, dst, $fu_args)
87   end
88   $total_files = $total_files + 1
89 end
90
91 def rename_files_keep_names(file)
92   proper_file = ""
93   if (file =~ /^[0-9. -]*(.*)$/) then
94     proper_file = $1
95   else
96     raise "can't find proper name for #{file}"
97   end
98   full_name = "#{get_file_name(1 + $total_files)} - #{proper_file}"
99   FileUtils.mv(file, full_name, $fu_args)
100   $total_files = $total_files + 1
101 end
102
103 # MAIN
104 begin
105   $opts = MyOptions.parse(ARGV)
106 rescue Exception => msg
107   $stderr.print("#{msg}.\nType --help to see usage information.\n")
108   exit 1
109 end
110
111 # make sure there aren't too many files
112 $total_files = 0
113 max_total_files = pow(10, $opts.num_digits) - 1
114 file_iter { |f| count_files(f) }
115 if ($total_files > max_total_files) then
116   raise "With #{$opts.num_digits} digit(s), we can only have at most \
117 #{max_total_files} files-- but there are #{$total_files} files in the \
118 #directory. Try setting a higher value for num_digits, using -D."
119 end
120
121 # rename files
122 $total_files = 0
123 if ( $opts.preserve_names ) then
124   file_iter { |f| rename_files_keep_names(f) }
125 else
126   file_iter { |f| rename_files(f) }
127 end
128
129 exit 0