Created
March 18, 2014 05:16
-
-
Save nacookan/9613947 to your computer and use it in GitHub Desktop.
今日の日付で自動的にフォルダを作る
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 | |
if ARGV[0] != nil | |
if File.exists?(ARGV[0]) | |
if FileTest.directory?(ARGV[0]) | |
path = ARGV[0] | |
else | |
abort ARGV[0] + " is not a directory." | |
end | |
else | |
abort ARGV[0] + " not found." | |
end | |
else | |
path = Dir.pwd | |
end | |
date_format = "%Y%m%d" | |
yesterday = (Time.now - 60 * 60 * 24).strftime(date_format) | |
(1..99).each do |i| | |
dir = File.join(path, sprintf("%s%02d", yesterday, i)) | |
if File.exists?(dir) | |
if Dir.entries(dir).join == "..." | |
Dir.rmdir(dir) | |
puts dir + " : exists, empty -> removed" | |
end | |
end | |
end | |
today = Time.now.strftime(date_format) | |
(1..99).each do |i| | |
dir = File.join(path, sprintf("%s%02d", today, i)) | |
if File.exists?(dir) | |
if Dir.entries(dir).join == "..." | |
puts dir + " : exists, empty" | |
exit | |
else | |
puts dir + " : exists, not-empty" | |
next | |
end | |
else | |
Dir.mkdir(dir) | |
puts dir + " : not-exists -> created" | |
exit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment