Sha256: 2b58fc07c2c4674443f5e175c1669f38d76420243db4edba9ee097fcf717c0d5

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# Copyright:: (c) Autotelik Media Ltd 2011
# Author ::   Tom Statter
# Date ::     Feb 2011
# License::   MIT
#
# Usage::     rake ar_loader:file_rename input=/blah image_load input=path_to_images
#
namespace :ar_loader do

  desc "copy or mv a folder of files, consistently renaming in the process"
  task :file_rename, :input, :offset, :prefix, :width, :commit, :mv do |t, args|
    raise "USAGE: rake file_rename input='C:\blah' [offset=n prefix='str' width=n]" unless args[:input] && File.exists?(args[:input])
    width = args[:width] || 2

    action = args[:mv] ? 'mv' : 'cp'

    cache = args[:input]

    if(File.exists?(cache) )
      puts "Renaming files from #{cache}"
      Dir.glob(File.join(cache, "*")) do |name|
        path, base_name = File.split(name)
        id = base_name.slice!(/\w+/)

        id = id.to_i + args[:offset].to_i if(args[:offset])
        id = "%0#{width}d" % id.to_i if(args[:width])
        id = args[:prefix] + id.to_s if(args[:prefix])

        destination = File.join(path, "#{id}#{base_name}")
        puts "ACTION: #{action} #{name} #{destination}"

        File.send( action, name, destination) if args[:commit]
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ar_loader-1.0.0.0 tasks/file_tasks.rake
ar_loader-9.9.9 tasks/file_tasks.rake
ar_loader-0.0.9 tasks/file_tasks.rake
ar_loader-0.0.8 tasks/file_tasks.rake