Created
August 7, 2012 19:41
-
-
Save kreeger/3288729 to your computer and use it in GitHub Desktop.
Handy batch renaming for stuff I do, in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Batch processing utilities I've found handy. | |
# Can also be found at https://gist.github.com/3288729. | |
# Released under the WTFBPPL license, at http://tomlea.co.uk/WTFBPPL.txt. | |
require 'thor' | |
require 'logger' | |
require 'fileutils' | |
require 'chronic' | |
REGEX = %r{ (?<code> \w+) (?<date> \d{6}) (?<suffix> -?\w+)? (?<extension> \.\w+) }ix | |
class SonOfABatch < Thor | |
desc 'resequence /PATH/FOR/FILES/TO/RENAME', 'Resequences all files in a directory for a start date.' | |
method_option :start_date, type: :string, required: true, aliases: ['-s'], | |
desc: 'REQUIRED. The date at which to begin the renaming sequence.' | |
method_option :skip_sundays, type: :boolean, default: false, | |
desc: 'Optional. If Sundays should be skipped in the renaming. Default is false.' | |
method_option :skip_weekends, type: :boolean, default: false, | |
desc: 'Optional. If weekends should be skipped in the renaming. Default is false.' | |
method_option :new_code, type: :string, aliases: ['-c'], | |
desc: 'Optional. The new feature code to assign to the files.' | |
def resequence(directory) | |
log = Logger.new STDOUT | |
date = Chronic.parse options[:start_date] | |
if date.nil? | |
log.fatal "Did not recognize date: #{options[:start_date]}." | |
return nil | |
end | |
filedir = File.expand_path(directory) | |
backup(filedir) | |
files = files_in_dir(filedir) | |
log.info "Running resequencer for #{files.length} files in #{filedir}." | |
str = "Beginning on #{date}" | |
str << " and skipping Sundays" if options[:skip_sundays] | |
str << '.' | |
log.info str | |
Array(files).each do |file| | |
next if File.directory?(file) | |
date += (1*60*60*24) if (date.saturday? && options[:skip_weekends]) | |
date += (1*60*60*24) if (date.sunday? && (options[:skip_sundays] || options[:skip_weekends])) | |
match = REGEX.match file | |
code = options[:new_code] ? options[:new_code] : match[:code] | |
new_filename = [code, date.strftime('%y%m%d'),match[:suffix],match[:extension]].join | |
source = File.join filedir, file | |
FileUtils.mv source, File.join(filedir, new_filename) | |
date += (1*60*60*24) | |
end | |
log.info "Finished." | |
end | |
desc 'recode /PATH/FOR/FILES/TO/RENAME new_code', 'Renames all files in a directory for a new feature code.' | |
def recode(directory, new_code) | |
filedir = File.expand_path directory | |
backup(filedir) | |
files = files_in_dir(filedir) | |
files.each do |file| | |
source = File.join filedir, file | |
match = REGEX.match file | |
new_filename = file.gsub(match[:code], new_code) | |
FileUtils.mv source, File.join(filedir, new_filename) | |
end | |
end | |
desc 'resuffix /PATH/FOR/FILES/TO/RENAME -newsuffix', 'Resuffixes all files in a directory for a new suffix.' | |
def resuffix(directory, new_suffix) | |
suffix = new_suffix.start_with?('-') ? new_suffix : "-#{new_suffix}" | |
filedir = File.expand_path directory | |
backup(filedir) | |
files = files_in_dir(filedir) | |
files.each do |file| | |
source = File.join filedir, file | |
match = REGEX.match file | |
new_filename = file.gsub(match[:suffix], suffix) | |
FileUtils.mv source, File.join(filedir, new_filename) | |
end | |
end | |
protected | |
def files_in_dir(directory) | |
Dir.entries(directory).reject {|f| %r{^\.}.match(f) } | |
end | |
def backup(directory) | |
FileUtils.cp_r(directory, "#{directory}-bak") | |
end | |
end | |
SonOfABatch.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment