Skip to content

Instantly share code, notes, and snippets.

@kizashi1122
Created October 2, 2014 07:49
Show Gist options
  • Save kizashi1122/2da0edb780b2e965ecb6 to your computer and use it in GitHub Desktop.
Save kizashi1122/2da0edb780b2e965ecb6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# 1 dir にフラットに1日になんファイルもたまっていくディレクトリに
# 対して、ファイルのタイムスタンプから yyyyMMdd フォルダを作って
# ファイルの移動をおこなう
#
require 'fileutils'
def move_files(dir, fpattern)
yesterday = Time.now - (60 * 60 * 24)
countf = 0
Dir::chdir(dir)
Dir::glob('*.*') do |f|
next unless f.match fpattern
mt = File.mtime(f)
next if mt > yesterday
dirname = mt.strftime("%Y%m%d")
Dir.mkdir dirname unless Dir.exist? dirname
FileUtils.mv f, dirname
# p "move #{f} #{dirname}"
countf += 1
end
p "(#{dir}) #{countf} files moved."
end
# logs
move_files('/path/to/logs', /\Ahogehoge*log\z/)
# eml files
move_files('/path/to/Maildir/new', /\A[a-zA-Z0-9\-]+\.eml\z/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment