Sha256: 58f275cb09039a3703178ff1f7a89896eb17fc68fa67e622511675d041d27f32

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

#! /usr/bin/env ruby
# coding: utf-8

USAGE = "Use --help option to see help."


##op.on("-g length", "--greed=length", "Greed matching for directory at least of the length."){|v| OPTIONS[:greed] = v.to_i}

require "pp"
gem "builtinextension"
require "string/escapezsh.rb"
require "filerenamer.rb"

class Array
  # return a length of common part of strings.
  def common_length
    index = 0
    min_length = self.min_by{|item| item.size}.size
    min_length.times do |char_index|
      1.upto(self.size-1) do |str_index|
        char_0 = self[0][char_index]
        char_n = self[str_index][char_index]
        return char_index if char_0 != char_n
      end
    end
    return min_length
  end
end

#p ['abc', 'abd', 'abe'].common_length #should be 2
#p ['abc', 'abd', 'ab'].common_length #should be 2
#p ['abcd', 'abce', 'abcde'].common_length #should be 2
#p ['biblio', 'bin', 'binary'].common_length #should be 2
#exit

OPTIONS = {}
op = FileRenamer::OptionParser.new
op.on("-l number", "--length=number", "Indicate length of initial characters. Default number = 1."){|v| OPTIONS[:length] = v.to_i}
op.parse!(ARGV)
OPTIONS[:length] ||= 1


if OPTIONS[:length] == 0 
  #print USAGE
  puts "0 cannot be used as a value of --length option."
  exit
end

files = ARGV
files = Dir.glob("*").sort if files.empty?

#pp files
conversions = {}
group_strs = files.map {|str| str[0...(OPTIONS[:length])]}.uniq
group_strs.each do |str|
  files = Dir.glob("#{str}*")
  #pp files
  length = files.common_length
  next if files.size == 1
  files.each do |old_filename|
    dirname = old_filename[0...length]
    filename= old_filename[length...(old_filename.size)]
    newname = dirname + "/" + filename

    next if filename.empty?
    conversions[old_filename] = newname
  end
end
#pp conversions

fr = FileRenamer::Commander.new(op.options, conversions.keys)
fr.execute do |old_name|
  conversions[old_name]
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
filerenamer-0.0.10 bin/classify_greed
filerenamer-0.0.9 bin/classify_greed
filerenamer-0.0.8 bin/classify_greed
filerenamer-0.0.7 bin/classify_greed