Last active
October 24, 2016 15:52
-
-
Save mmistakes/36e5877cd9a46580b86df2349981f1dc to your computer and use it in GitHub Desktop.
Rename comment data files eg: comment-2014-01-21013323.yml ~>comment-1390268003000.yml
This file contains hidden or 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
require "bundler/setup" | |
require "fileutils" | |
require "date" | |
require "time" | |
## -- Misc Configs -- ## | |
source_dir = "src" # source file directory | |
comments_dir = "_data/comments/sub-folder-name" # comment directory | |
comment_label = "comment-" | |
comment_ext = ".yml" | |
desc "Convert ISO date in comment file names to UNIX timestamps" | |
task :rename_comments do | |
Dir.chdir("#{source_dir}/#{comments_dir}") do | |
Dir['*.yml'].each do |comment| | |
comment_date = "" | |
File.open( comment ) do |f| | |
f.grep( /^date: / ) do |line| | |
comment_date = Time.parse(line).to_i | |
comment_date = (comment_date.to_f * 1000).round.to_s | |
break | |
end | |
end | |
new_comment_name = comment_label + comment_date + comment_ext # determining the correct filename | |
if comment !~ /comment-(\d)*.yml/ # only rename ISO datetime file names (eg. comment-2016-10-24-123456.yml) | |
puts "renaming #{comment} to #{new_comment_name}" | |
FileUtils.mv(comment, new_comment_name) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment