Skip to content

Commit 7db4764

Browse files
committed
Added exificator rename action
1 parent d6a04cb commit 7db4764

File tree

2 files changed

+34
-87
lines changed

2 files changed

+34
-87
lines changed

exificator.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def process
5656
next if new_description.empty?
5757
exiftool.descriptions = new_description
5858

59-
rename(image, new_description)
59+
renameWithDescription(image, new_description)
6060
end
6161
end
6262

@@ -84,13 +84,39 @@ def convert
8484
end
8585
end
8686

87+
option :prefix, type: :string, desc: 'Prefix to use. Defaults to last modified date in YYYYMMDDHHMMSS_ format.'
88+
desc 'rename', 'Renames in format <prefix>_<description>.<ext> if there is a description.'
89+
def rename
90+
if images.size == 0
91+
puts "No images found."
92+
return
93+
end
94+
95+
unless Prompt.confirm?("Are you sure you want to rename #{images.size} images in the format of #{options[:prefix]||'YYYYMMDDHHMMSS'}_<description>? (y/n)")
96+
puts "Aborting rename!"
97+
return
98+
end
99+
100+
images.each.with_index do |image,i|
101+
prefix = options[:prefix] || modified_at(image).strftime('%Y%m%d%H%M%S');
102+
ext = File.extname(image)
103+
dir = File.dirname(image)
104+
description = ExifTool.new(image).get('Description')
105+
descName = (description.empty? ? '' : '_'+description_to_name(description))
106+
newPath = File.join(dir,"#{prefix}#{descName}#{ext}")
107+
108+
puts "#{i+1}/#{images.size}: Renaming #{image} to #{newPath}"
109+
FileUtils.move(image, newPath)
110+
end
111+
end
112+
87113
private
88114

89115
def description_to_name(description)
90116
description.strip.gsub(PUNCTUATION_REGEX,'').gsub(/\s+/,'_').gsub(/\_+/,'_')
91117
end
92118

93-
def rename(path, description)
119+
def renameWithDescription(path, description)
94120
name = description_to_name(description)
95121
return if name.empty?
96122

@@ -138,8 +164,8 @@ def verbose?
138164
def images
139165
return @images if @images
140166

141-
if path && File.file?(path)
142-
@images = [path]
167+
if path
168+
@images = (File.file?(path) ? [path] : [])
143169
return @images
144170
end
145171

@@ -169,6 +195,10 @@ def detail(image)
169195
def open(image_list)
170196
`open -a Preview '#{image_list}'`
171197
end
198+
199+
def modified_at(path)
200+
return File.mtime(path)
201+
end
172202
end
173203

174204
begin

name2date.rb

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)