X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=names_to_numbers.rb;h=31c42e449908130efb0efadd1993f216cf57108f;hb=c1316a732ba6d2dea7c73c7c603be523eb6946da;hp=689e6de61298ff8ce59822602f16df33be8992a0;hpb=48535204969cc0fdb31be8f397b9dcdb8ed86763;p=cmccabe-bin diff --git a/names_to_numbers.rb b/names_to_numbers.rb index 689e6de..31c42e4 100755 --- a/names_to_numbers.rb +++ b/names_to_numbers.rb @@ -19,7 +19,10 @@ class MyOptions opts.dry_run = false opts.num_digits = 2 opts.extension = nil + opts.starting_number = 1 + opts.prefix_counter_increment = 1 $fu_args = { :verbose => true } + opts.preserve_names = false # Fill in $opts values parser = OptionParser.new do |myparser| @@ -38,12 +41,31 @@ class MyOptions "The file extension for the files to rename.") do |e| opts.extension = e end + myparser.on("--preserve-names", "-p", + "Preserve the names while changing the numbers.") do |e| + opts.preserve_names = true + end + myparser.on("--starting-number NUMBER", "-N", + "The starting number (defaults to 1)") do |e| + opts.starting_number = e.to_i + end + myparser.on("--glob GLOB", "-g", + "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) raise "invalid num_digits: #{opts.num_digits}" unless opts.num_digits > 0 - raise "must give an extension" unless opts.extension != nil + raise "must specify an extension" unless opts.extension != nil + if (opts.glob == nil) then + opts.glob = "*.#{opts.extension}" + end return opts end end @@ -55,9 +77,8 @@ def pow(x, y) end return ret end -#.#{$opts.extension}").sort.each do |f| def file_iter - Dir.glob("*.#{$opts.extension}").sort.each do |f| + Dir.glob($opts.glob).sort.each do |f| yield f end end @@ -67,12 +88,27 @@ def count_files(file) end def get_file_name(num) - return sprintf("%0#{$opts.num_digits}d.#{$opts.extension}", num) + return sprintf("%0#{$opts.num_digits}d", num) end def rename_files(file) - FileUtils.mv(file, get_file_name(1 + $total_files), $fu_args) - $total_files = $total_files + 1 + dst="#{get_file_name($prefix_counter)}.#{$opts.extension}" + if (file != dst) then + FileUtils.mv(file, dst, $fu_args) + end + $prefix_counter = $prefix_counter + $opts.prefix_counter_increment +end + +def rename_files_keep_names(file) + proper_file = "" + if (file =~ /^[0-9. -]*(.*)$/) then + proper_file = $1 + else + raise "can't find proper name for #{file}" + end + full_name = "#{get_file_name($prefix_counter)} - #{proper_file}" + FileUtils.mv(file, full_name, $fu_args) + $prefix_counter = $prefix_counter + $opts.prefix_counter_increment end # MAIN @@ -94,7 +130,11 @@ if ($total_files > max_total_files) then end # rename files -$total_files = 0 -file_iter { |f| rename_files(f) } +$prefix_counter = $opts.starting_number +if ( $opts.preserve_names ) then + file_iter { |f| rename_files_keep_names(f) } +else + file_iter { |f| rename_files(f) } +end exit 0