Ruby批量修改文件创建时间


电脑里面的相片,因为复制来,复制去的.时间不太对.然后传到iphone就乱了.那边是以创建时间什么的来排序.然后折腾了一个脚本改创建时间.网上搜索了一下.没看见有什么好办法可以改. 只有使用一个比较SB的方法了.

首先把相片的exif信息中的时间取出,然后把系统时间改为这个时间.再复制一下.OK

考虑有的jpg可能没有拍摄时间,就以创建时间和修改时间去比较,按比较小的来。

重要文章阅读:Ruby入门--Linux/Windows下的安装、代码开发及Rails实战

Ruby on rails初体验系列文章:



《Ruby for Rails中文版》.((美)David Black ).[PDF]

Ubuntu 13.04下Ruby的安装

代码如下:

require 'exifr'
require 'fileutils'
require 'find'
dist_dir = "D:/Old/Pic"
 
def chang_ctime(name)
temp_dir = File.join(name.split('/')[0..-2]).sub('Old','New')
unless Dir.exist?(temp_dir)
FileUtils.mkdir_p(temp_dir)
end
if File.file?(name)
if name[-4..-1].downcase == '.jpg'
temp_date = EXIFR::JPEG.new(name).date_time_original.to_s
if temp_date.size != 0
temp_date = temp_date.split
`date #{temp_date[0]}`
`time #{temp_date[1]}`
FileUtils.cp name, name.sub("Old","New")
else
if File.ctime(name) > File.mtime(name)
temp_date = File.mtime(name).to_s.split
`date #{temp_date[0]}`
`time #{temp_date[1]}`
else
temp_date = File.ctime(name).to_s.split
`date #{temp_date[0]}`
`time #{temp_date[1]}`
end
FileUtils.cp name, name.sub("Old","New")
end
else
if File.ctime(name) > File.mtime(name)
temp_date = File.mtime(name).to_s.split
`date #{temp_date[0]}`
`time #{temp_date[1]}`
else
temp_date = File.ctime(name).to_s.split
`date #{temp_date[0]}`
`time #{temp_date[1]}`
end
FileUtils.cp name, name.sub("Old","New")
end
end
end
 
Find.find(dist_dir) do |path|
chang_ctime(path)
end
 
puts "OK"

本文永久更新链接地址: 

相关内容