-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-number.rb
More file actions
35 lines (30 loc) · 1023 Bytes
/
batch-number.rb
File metadata and controls
35 lines (30 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
options = []
@exluded_numbers = []
ARGV.options do |opts|
opts.on('-s', '--start=val', Integer) { |val| Start = val }
opts.on('-e', '--end=val', Integer) { |val| End = val }
opts.on('-x', '--exclude=val', Integer) { |val| @exluded_numbers << val }
opts.parse!
end
target_dir = ARGV[0]
images = Dir.glob("#{target_dir}/*.[Jj][Pp][Gg]")
imagesSorted = images.sort_by {|filename| File.mtime(filename) }
target_numbers = (Start .. End).to_a - @exluded_numbers
if ! defined?(images) || images.empty?
puts "Please add directory containing image files!"
exit
elsif target_numbers.count != imagesSorted.count
puts "Number of images and new file names doesn't match! Please check inputs."
exit
end
imagesSorted.each_with_index do |image,index|
newName = File.dirname(image) + '/' + target_numbers[index].to_s + '.jpg'
unless File.exist?(newName)
FileUtils.mv(image,newName)
else
puts "File #{newName} already exists! Skipping!"
end
end