Sha256: 42c0d340037062630c92dff83606b7692ab72bbf0da83e941dc24902db30924a

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

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

# rename using serial number.
# 拡張子は保存
# -i オプションで開始番号を指定
# -p : preserve old name 元のファイル名を残す。foo.txt -> 001-foo.txt
require 'pp'
require 'filerenamer/commander.rb'
require 'filerenamer/optionparser.rb'


OPTIONS = {}
op = FileRenamer::OptionParser.new
op.on("-i num", "--initial", "Indicate initial number."){ |val|
  OPTIONS[:init] = val.to_i
}
op.on("-p", "--preserve", "Preserve old name."){
  OPTIONS[:preserve] = true
}
op.on("-r", "--random", "Random number."){
  OPTIONS[:random] = true
}
op.parse!(ARGV)

OPTIONS[:init] ||= 0

files = FileRenamer::Commander.files(ARGV)
last_number = OPTIONS[:init].to_i +  files.size - 1
width = last_number.to_s.size

files.shuffle!  if OPTIONS[:random]

fr = FileRenamer::Commander.new(op.options, files)

fr.execute do |filename|
  @counter ||= OPTIONS[:init]
  if (OPTIONS[:preserve])
    new_name = sprintf("%0#{width}d-%s", @counter, filename)
  else 
    extention = File.extname(filename)
    new_name = sprintf("%0#{width}d%s", @counter, extention)
  end
  @counter += 1
  new_name
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filerenamer-0.0.9 bin/rennum
filerenamer-0.0.8 bin/rennum