-
Notifications
You must be signed in to change notification settings - Fork 10
/
canonify_imgnames
executable file
·63 lines (51 loc) · 1.87 KB
/
canonify_imgnames
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
# Copyright 2016--2024, Raphael Reitzig
#
# canonify_imgnames is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# canonify_imgnames is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with canonify_imgnames. If not, see <http://www.gnu.org/licenses/>.
# Depends on `exiftool`
require 'fileutils'
DEBUG = false unless Object.const_defined?(:DEBUG)
DRY_RUN = false unless Object.const_defined?(:DRY_RUN)
require_relative 'canonical_imgname'
renamed = 0
ARGV.each { |file|
# Normalize so comparisons work out well later.
file = full_filename(file, File.basename(file, File.extname(file)))
if File.exist?(file) && !File.directory?(file)
puts file if DEBUG
new_name = canonical_image_name(file)
if new_name.nil?
puts " - No better name found, skipping.\n\n" if DEBUG
next
elsif file == full_filename(file, new_name)
puts " - Already has good name, skipping.\n\n" if DEBUG
next
end
# Resolve duplicates
i = 1
candidate = new_name
while File.exist?(full_filename(file, candidate))
puts " - '#{candidate}' already taken" if DEBUG
candidate = "#{new_name} #{i.to_s}"
i += 1
end
new_name = candidate
# Finally, rename!
puts " - Renaming to: #{full_filename(file, new_name)}" if DEBUG
FileUtils::mv(file, full_filename(file, new_name)) unless DRY_RUN
renamed += 1
end
puts "" if DEBUG
}
puts "Renamed #{renamed}, skipped #{ARGV.size - renamed}"