names_to_numbers.rb: add --prefix-counter-increment, fix bug in --starting_number
authorColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Mon, 13 Oct 2014 00:33:21 +0000 (17:33 -0700)
committerColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Mon, 13 Oct 2014 00:34:47 +0000 (17:34 -0700)
* add --prefix-counter-increment

* fix bug where --starting_number did not take effect when preserving
the names of files.

Signed-off-by: Colin McCabe <cmccabe@alumni.cmu.edu>

names_to_numbers.rb

index fe03ff9..31c42e4 100755 (executable)
@@ -20,6 +20,7 @@ class MyOptions
     opts.num_digits = 2
     opts.extension = nil
     opts.starting_number = 1
+    opts.prefix_counter_increment = 1
     $fu_args = { :verbose => true }
     opts.preserve_names = false
 
@@ -52,6 +53,10 @@ class MyOptions
               "Specify the glob expression to use.  Example: '*/*.mp3'.") do |d|
         opts.glob = d
       end
+      myparser.on("--prefix-counter-increment NUMBER", "-I",
+              "The increment to use (defaults to 1)") do |e|
+        opts.prefix_counter_increment = e.to_i
+      end
     end
 
     parser.parse!(args)
@@ -87,11 +92,11 @@ def get_file_name(num)
 end
 
 def rename_files(file)
-  dst="#{get_file_name($opts.starting_number + $total_files)}.#{$opts.extension}"
+  dst="#{get_file_name($prefix_counter)}.#{$opts.extension}"
   if (file != dst) then
     FileUtils.mv(file, dst, $fu_args)
   end
-  $total_files = $total_files + 1
+  $prefix_counter = $prefix_counter + $opts.prefix_counter_increment
 end
 
 def rename_files_keep_names(file)
@@ -101,9 +106,9 @@ def rename_files_keep_names(file)
   else
     raise "can't find proper name for #{file}"
   end
-  full_name = "#{get_file_name(1 + $total_files)} - #{proper_file}"
+  full_name = "#{get_file_name($prefix_counter)} - #{proper_file}"
   FileUtils.mv(file, full_name, $fu_args)
-  $total_files = $total_files + 1
+  $prefix_counter = $prefix_counter + $opts.prefix_counter_increment
 end
 
 # MAIN
@@ -125,7 +130,7 @@ if ($total_files > max_total_files) then
 end
 
 # rename files
-$total_files = 0
+$prefix_counter = $opts.starting_number
 if ( $opts.preserve_names ) then
   file_iter { |f| rename_files_keep_names(f) }
 else