设为首页 加入收藏

TOP

Ruby批量修改文件创建时间
2014-11-23 23:16:51 来源: 作者: 【 】 浏览:7
Tags:Ruby 批量 修改 文件 创建 时间

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


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


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


Ruby on rails初体验系列文章:


代码如下:


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"


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言全局变量extern、static的含.. 下一篇Java中的枚举类型Enum

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: