From 24c0e76c1c9f8ba17e0028ccdf782d89bb238884 Mon Sep 17 00:00:00 2001 From: Colin Patrick Mccabe Date: Sun, 12 Oct 2014 17:33:21 -0700 Subject: [PATCH] names_to_numbers.rb: add --prefix-counter-increment, fix bug in --starting_number * add --prefix-counter-increment * fix bug where --starting_number did not take effect when preserving the names of files. Signed-off-by: Colin McCabe --- names_to_numbers.rb | 15 ++++++++++----- 1 files changed, 10 insertions(+), 5 deletions(-) diff --git a/names_to_numbers.rb b/names_to_numbers.rb index fe03ff9..31c42e4 100755 --- a/names_to_numbers.rb +++ b/names_to_numbers.rb @@ -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 -- 1.6.6.rc1.39.g9a42