module Renumber

Constants

VERSION

Public Instance Methods

change_files(path, prefix=nil, suffix=nil) click to toggle source
# File lib/renumber.rb, line 12
def change_files(path, prefix=nil, suffix=nil)
  sorted_files = sorted_files(path)
  return if sorted_files.length == 0

  zeros_length = Math.log10(sorted_files.length).ceil

  sorted_files.each_with_index do |file, index|
    File.rename(file, "#{prefix}#{index.to_s.rjust(zeros_length, '0')}#{suffix}")
  end
end
sorted_files(path) click to toggle source
# File lib/renumber.rb, line 4
def sorted_files(path)
  raise ArgumentError, 'Path must be a directory' unless File.directory?(path)

  Dir.chdir(path)

  file_entries_from_path('.').sort
end

Private Instance Methods

file_entries_from_path(path) click to toggle source
# File lib/renumber.rb, line 25
def file_entries_from_path(path)
  Dir.entries(path).select { |file| File.file?(file) }
end