Skip to content

Instantly share code, notes, and snippets.

@konklone
Created November 9, 2015 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konklone/21b570c7845e2cc9765e to your computer and use it in GitHub Desktop.
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
#!/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, eric.mill@gsa.gov. 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