ファイル操作

指定フォルダ以下のファイルを 年月別のフォルダにコピーする

このような画像フォルダがあって...


C:\>ruby c:\study\ruby\chapter005\lesson004.rb c:\study\picture
2008/01/01 00:00:00 c:\study\picture\100CDPFP\IMGA0001.JPG
2008/01/02 00:00:00 c:\study\picture\100CDPFP\IMGA0002.JPG
2008/01/03 00:00:00 c:\study\picture\100CDPFP\IMGA0003.JPG
2008/01/04 00:00:00 c:\study\picture\100CDPFP\IMGA0004.JPG
2008/01/05 00:00:00 c:\study\picture\100CDPFP\IMGA0005.JPG
2008/01/01 00:00:00 c:\study\picture\100KCA26\CA260001.JPG
2008/02/01 00:00:00 c:\study\picture\100KCA26\CA260002.JPG
2008/03/01 00:00:00 c:\study\picture\100KCA26\CA260003.JPG
2008/04/01 00:00:00 c:\study\picture\100KCA26\CA260004.JPG
2008/05/01 00:00:00 c:\study\picture\100KCA26\CA260005.JPG
2008/01/01 00:01:00 c:\study\picture\100msdcf\DSC00001.JPG
2008/01/01 00:02:00 c:\study\picture\100msdcf\DSC00002.JPG
2008/01/01 00:03:00 c:\study\picture\100msdcf\DSC00003.JPG
2008/01/01 00:04:00 c:\study\picture\100msdcf\DSC00004.JPG
2008/01/01 00:05:00 c:\study\picture\100msdcf\DSC00005.JPG
2008/02/01 00:00:00 c:\study\picture\101CDPFP\IMGA0001.JPG
2008/02/02 00:00:00 c:\study\picture\101CDPFP\IMGA0002.JPG
2008/02/03 00:00:00 c:\study\picture\101CDPFP\IMGA0003.JPG
2008/02/04 00:00:00 c:\study\picture\101CDPFP\IMGA0004.JPG
2008/02/05 00:00:00 c:\study\picture\101CDPFP\IMGA0005.JPG
2008/03/01 00:00:00 c:\study\picture\102CDPFP\IMGA0001.JPG
2008/03/02 00:00:00 c:\study\picture\102CDPFP\IMGA0002.JPG
2008/03/03 00:00:00 c:\study\picture\102CDPFP\IMGA0003.JPG
2008/03/04 00:00:00 c:\study\picture\102CDPFP\IMGA0004.JPG
2008/03/05 00:00:00 c:\study\picture\102CDPFP\IMGA0005.JPG

"¥年¥月¥日_連番.JPG" というフォルダ/ファイル名でコピーしたい


\100CDPFP\IMGA0001.JPG => \2008\01\01_00001
\100KCA26\CA260001.JPG => \2008\01\01_00002
\100msdcf\DSC00001.JPG => \2008\01\01_00003
\100msdcf\DSC00002.JPG => \2008\01\01_00004
\100msdcf\DSC00003.JPG => \2008\01\01_00005
\100msdcf\DSC00004.JPG => \2008\01\01_00006
\100msdcf\DSC00005.JPG => \2008\01\01_00007
\100CDPFP\IMGA0002.JPG => \2008\01\02_00001
\100CDPFP\IMGA0003.JPG => \2008\01\03_00001
\100CDPFP\IMGA0004.JPG => \2008\01\04_00001
\100CDPFP\IMGA0005.JPG => \2008\01\05_00001
\101CDPFP\IMGA0001.JPG => \2008\02\01_00001
\100KCA26\CA260002.JPG => \2008\02\01_00002
\101CDPFP\IMGA0002.JPG => \2008\02\02_00001
\101CDPFP\IMGA0003.JPG => \2008\02\03_00001
\101CDPFP\IMGA0004.JPG => \2008\02\04_00001
\101CDPFP\IMGA0005.JPG => \2008\02\05_00001
\102CDPFP\IMGA0001.JPG => \2008\03\01_00001
\100KCA26\CA260003.JPG => \2008\03\01_00002
\102CDPFP\IMGA0002.JPG => \2008\03\02_00001
\102CDPFP\IMGA0003.JPG => \2008\03\03_00001
\102CDPFP\IMGA0004.JPG => \2008\03\04_00001
\102CDPFP\IMGA0005.JPG => \2008\03\05_00001
\100KCA26\CA260004.JPG => \2008\04\01_00001
\100KCA26\CA260005.JPG => \2008\05\01_00001

lesson006.rb


require 'fileutils.rb'

def putsDir(path, h)
if FileTest.directory?(path)
Dir.open(path) do |dir|
dir.each do |name|
next if name == "." # 自分
next if name == ".." # 親

putsDir path + "/" + name, h
end
end
else
if File.extname(path).downcase == ".jpg"
h[path.gsub(/\//, "\\")] = File.mtime(path).strftime("%Y/%m/%d %H:%M:%S")
end
end
end

if FileTest.exist?(ARGV[1])
FileUtils.rm_r(ARGV[1])
end
Dir.mkdir ARGV[1]

h = Hash.new
putsDir(ARGV[0], h)
h = h.sort_by { |key, value| value }

y_save = ""
m_save = ""
d_save = ""
seq = 0

h.each do |key, value|
array = value.split(/[\/ ]/)
y = array[0]
m = array[1]
d = array[2]

dirName = ARGV[1] + "\\" + y
if y_save != y
y_save = y
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

dirName = dirName + "\\" + m
if m_save != m
m_save = m
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

if d_save != d
d_save = d
seq = 1
else
seq += 1
end

fileName = dirName + "\\" + d + "_" + format("%.5d", seq) + ".jpg"

FileUtils.cp key, fileName
end

実行結果


C:\>ruby c:\study\ruby\chapter005\lesson006.rb c:\study\picture c:\study\picture
_save

C:\>ruby c:\study\ruby\chapter005\lesson004.rb c:\study\picture_save
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00002.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00003.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00004.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00005.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00006.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\01_00007.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\02_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\03_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\04_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\01\05_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\01_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\01_00002.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\02_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\03_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\04_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\02\05_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\01_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\01_00002.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\02_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\03_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\04_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\03\05_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\04\01_00001.jpg
2008/07/30 11:19:45 c:\study\picture_save\2008\05\01_00002.jpg

更新時刻が、変更されてしまった!

lesson007.rb


require 'fileutils.rb'

def putsDir(path, h)
if FileTest.directory?(path)
Dir.open(path) do |dir|
dir.each do |name|
next if name == "." # 自分
next if name == ".." # 親

putsDir path + "/" + name, h
end
end
else
if File.extname(path).downcase == ".jpg"
h[path.gsub(/\//, "\\")] = File.mtime(path).strftime("%Y/%m/%d %H:%M:%S")
end
end
end

if FileTest.exist?(ARGV[1])
FileUtils.rm_r(ARGV[1])
end
Dir.mkdir ARGV[1]

h = Hash.new
putsDir(ARGV[0], h)
h = h.sort_by { |key, value| value }

y_save = ""
m_save = ""
d_save = ""
seq = 0

h.each do |key, value|
array = value.split(/[\/ :]/)
y = array[0]
m = array[1]
d = array[2]
h = array[3]
n = array[4]
s = array[5]

dirName = ARGV[1] + "\\" + y
if y_save != y
y_save = y
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

dirName = dirName + "\\" + m
if m_save != m
m_save = m
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

if d_save != d
d_save = d
seq = 1
else
seq += 1
end

fileName = dirName + "\\" + d + "_" + format("%.5d", seq) + ".jpg"

FileUtils.cp key, fileName

atime = mtime = Time.mktime(y, m, d, h, n, s)
File.utime(atime, mtime, fileName)
end

実行結果

C:\>ruby c:\study\ruby\chapter005\lesson007.rb c:\study\picture c:\study\picture
_save

C:\>ruby c:\study\ruby\chapter005\lesson004.rb c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00001.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00002.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_00003.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_00004.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_00005.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_00006.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_00007.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_00001.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_00001.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_00001.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00002.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_00001.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_00001.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_00001.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00002.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_00001.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_00001.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_00001.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_00001.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_00001.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_00002.jpg

やばい、バグってる...
lesson008.rb


require 'fileutils.rb'

def putsDir(path, h)
if FileTest.directory?(path)
Dir.open(path) do |dir|
dir.each do |name|
next if name == "." # 自分
next if name == ".." # 親

putsDir path + "/" + name, h
end
end
else
if File.extname(path).downcase == ".jpg"
h[path.gsub(/\//, "\\")] = File.mtime(path).strftime("%Y/%m/%d %H:%M:%S")
end
end
end

if FileTest.exist?(ARGV[1])
FileUtils.rm_r(ARGV[1])
end
Dir.mkdir ARGV[1]

h = Hash.new
putsDir(ARGV[0], h)
h = h.sort_by { |key, value| value }

y_save = ""
m_save = ""
d_save = ""
seq = 0

h.each do |key, value|
array = value.split(/[\/ :]/)
y = array[0]
m = array[1]
d = array[2]
h = array[3]
n = array[4]
s = array[5]

if (y_save != y) || (m_save != m) || (d_save != d)
seq = 1
else
seq += 1
end

dirName = ARGV[1] + "\\" + y
if y_save != y
y_save = y
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

dirName = dirName + "\\" + m
if m_save != m
m_save = m
if FileTest.exist?(dirName)
FileUtils.rm_r(dirName)
end
Dir.mkdir dirName
end

if d_save != d
d_save = d
end

fileName = dirName + "\\" + d + "_" + format("%.5d", seq) + ".jpg"

FileUtils.cp key, fileName

atime = mtime = Time.mktime(y, m, d, h, n, s)
File.utime(atime, mtime, fileName)
end

実行結果

C:\>ruby c:\study\ruby\chapter005\lesson008.rb c:\study\picture c:\study\picture
_save

C:\>ruby c:\study\ruby\chapter005\lesson004.rb c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00001.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00002.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_00003.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_00004.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_00005.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_00006.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_00007.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_00001.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_00001.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_00001.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00002.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_00001.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_00001.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_00001.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00002.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_00001.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_00001.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_00001.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_00001.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_00001.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_00001.jpg