Created
November 9, 2015 21:29
-
-
Save konklone/21b570c7845e2cc9765e to your computer and use it in GitHub Desktop.
Rename files exported from L-Soft's LISTSERV software from XXX.LOGYYMM to XXX-YYYYMM.LOG
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 | |
### | |
# Small script to rename files as exported from L-Soft's LISTSERV. | |
# | |
# Files are named with the month in the extension, e.g. SM-COP.LOG1410 | |
# This renames them to be of the form SM-COP-2014-10.log | |
# | |
# Written by Eric Mill, [email protected]. Public domain. | |
### | |
require 'fileutils' | |
# Take in directory full of .log[\d\d\d\d] files. | |
dir = ARGV[0] | |
# Prepend to 2-digit years. Update this value as the millenia pass. | |
century = "20" | |
# Rename them each in-place. This is a destructive action. Have a backup. | |
renamed = 0 | |
Dir.glob("#{dir}/*").each do |path| | |
name = File.basename(path) | |
unless (matches = name.match(/^(.*?)(\.LOG)(\d\d)(\d\d)$/i)) | |
puts "NO MATCH: #{name}" | |
next | |
end | |
listname = matches[1].upcase | |
extension = matches[2].downcase | |
year = "#{century}#{matches[3]}" | |
month = matches[4] | |
new_filename = "#{listname}-#{year}-#{month}#{extension}" | |
puts "Renaming #{name} to #{new_filename}." | |
FileUtils.mv(File.join(dir, name), File.join(dir, new_filename)) | |
renamed += 1 | |
end | |
puts "Renamed #{renamed} files in-place." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment