-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo_copy.rb
99 lines (65 loc) · 2.24 KB
/
photo_copy.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'progressbar.rb'
def Usage()
puts "Usage: $0 <source dir> <target dir>"
end
def generate_year_directory(filename)
s = File.stat filename
return sprintf("%04d", s.mtime.year)
end
def generate_day_directory(filename)
s = File.stat filename
return sprintf("%04d %02d %02d", s.mtime.year, s.mtime.month, s.mtime.day)
end
def generate_target_directory(sourceFile, dest)
return "#{dest}/#{generate_year_directory(sourceFile)}/#{generate_day_directory(sourceFile)}"
end
if ($*.length != 2) then
Usage()
exit(1)
end
source = $*[0]
dest = $*[1]
if ( not (File.directory? source and File.directory? dest) ) then
Usage()
exit(1)
end
#
# main
#
Dir.open( source ) do |d|
valid_files = d.select do |entry|
sourceFile = "#{source}/#{entry}"
destFile = "#{generate_target_directory(sourceFile, dest)}/#{entry}"
File.exist?(sourceFile) and
File.file?(sourceFile) and
(entry != "Thumbs.db") and
not File.exist?(destFile)
end
counter = 1
if ( valid_files.length == 0 ) then
puts "No files to copy!"
elsif ( 0 < (difference = (d.entries.length - valid_files.length)))
puts "Skipping #{difference} files that are invalid or already exist"
end
# valid_files.each do |entry|
progress_bar( valid_files ) do |entry|
sourceFile = "#{source}/#{entry}"
targetDir = generate_target_directory(sourceFile, dest)
targetFile = "#{targetDir}/#{entry}"
if (not File.directory? "#{dest}/#{generate_year_directory(sourceFile)}" ) then
Dir.mkdir "#{dest}/#{generate_year_directory(sourceFile)}"
end
if (not File.directory? targetDir) then
Dir.mkdir targetDir
end
message="Copying #{counter} of #{valid_files.length}; #{targetFile}... "
col=`/usr/bin/tput cols`
print message.slice(0,col.to_i-15)
$stdout.flush
# `xcopy /F /Y /I \"#{sourceFile}\" \"#{targetDir}\"` #windows-specific
`/bin/cp --preserve=timestamps \"#{sourceFile}\" \"#{targetDir}\"`
#print "\r" # carriage return
#$stdout.flush
counter += 1
end
end