Created
July 21, 2014 07:49
-
-
Save Shinya131/db63001992286b4c4057 to your computer and use it in GitHub Desktop.
current directoryにあるファイル/フォルダをArchive directoryにmvする。"#{HOME}/.put_away_ignore"を記述することで移動対象外のファイルを指定できる。
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
require 'pry' | |
def target_root_dir | |
"./" | |
end | |
# make archive directorty | |
def archive_dir_name | |
dir = Time.now.strftime("%Y_%m_%d") | |
File.join(target_root_dir, dir) | |
end | |
def make_dir_for_archive | |
if File.exist?(archive_dir_name) | |
raise "archive dirctory already exits. dir_name: #{archive_dir_name}" | |
end | |
Dir.mkdir(archive_dir_name) | |
end | |
# move to archive dirctory | |
def target_files | |
Dir.glob(File.join(target_root_dir, "*")).map do |path| | |
File.basename(path) | |
end | |
end | |
def move_to_archive(source) | |
to = File.join(archive_dir_name,source) | |
File.rename(source, to) | |
end | |
def read_ignore_config | |
config_file = File.join(ENV["HOME"], ".put_away_ignore") | |
unless File.exist?(config_file) | |
return [] | |
end | |
File.open(config_file).read.split("\n") | |
end | |
def ignore_file_names | |
ignore_file_names = [] | |
ignore_file_names << File.basename(archive_dir_name) | |
ignore_file_names += read_ignore_config | |
@ignore_file_names ||= ignore_file_names | |
end | |
make_dir_for_archive | |
target_files.each do |file| | |
next if ignore_file_names.include?(File.basename file) | |
move_to_archive(file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment