Sha256: 8f0fef2d42c29d018192cf2dda7f35a8acbb4c348c21412f54c9dba5627b0875

Contents?: true

Size: 662 Bytes

Versions: 4

Compression:

Stored size: 662 Bytes

Contents

require 'renumber/version'

module Renumber
  def sorted_files(path)
    raise ArgumentError, 'path must be a directory' unless File.directory?(path)

    Dir.chdir(path)

    file_entries_from_path('.').sort
  end

  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

  private

  def file_entries_from_path(path)
    Dir.entries(path).select { |file| File.file?(file) }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
renumber-0.0.7 lib/renumber.rb
renumber-0.0.6 lib/renumber.rb
renumber-0.0.5 lib/renumber.rb
renumber-0.0.4 lib/renumber.rb