ruby批量修改文件创建时间

简介:

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


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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"


本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1421246,如需转载请自行联系原作者
相关文章
|
Ruby
|
Ruby
|
Ruby
Ruby 教程 之 Ruby 文件的输入与输出 8
Ruby 文件的输入与输出
102 0
|
Ruby
|
Ruby
|
Ruby
|
Ruby
Ruby 教程 之 Ruby 文件的输入与输出 7
Ruby 文件的输入与输出
113 1