-
Notifications
You must be signed in to change notification settings - Fork 0
/
datcheck.rb
77 lines (67 loc) · 2.09 KB
/
datcheck.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
require "fileutils"
#require 'byebug'
# usage example datcheck.rb newpack/TOSEC/
if ARGV.length == 1
folder = ARGV[0]
else
puts "Usage: ruby datcheck.rb [dats_folder]"
puts "Example: ruby datcheck.rb newpack/TOSEC/"
exit 1
end
dir = FileUtils.makedirs "outdated"
ARGV.each do|a|
puts "Argument: #{a}"
end
def escape_glob(s)
s.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
end
class String
def to_path(end_slash=false)
"#{'/' if self[0]=='\\'}#{self.split('\\').join('/')}#{'/' if end_slash}"
end
end
folder = folder.to_path(true)
datfiles = Dir["#{folder}*.dat"]
duplicated = 0
split = 0
puts "-------------------------"
datfiles.each do | dat |
#puts "Datfile: #{dat}"
filename = File.basename(dat)
idx = filename.index("(TOSEC-")
partname = filename[0..idx+6]
found = Dir["#{folder}#{escape_glob(partname)}*.dat"]
if found.length > 1
duplicated = duplicated + 1
puts "Found unremoved updates:"
found.each {|f| puts File.basename f}
found_sorted = found.sort.reverse!
found_sorted.delete_at(0)
#byebug
FileUtils.mv found_sorted, dir.first
puts "Moved datfiles (to #{dir.first}):"
found_sorted.each { |f| puts f}
puts "-------------------------"
end
# check for possible problem of dat (tosec) where dat - another category (tosec) exists
partname = filename[0..idx-1]
#puts partname
found = Dir["#{folder}#{escape_glob(partname)}-*.dat"]
#puts "#{folder}#{escape_glob(partname)}-*.dat"
#puts found
if (found.length >= 1)
split = split + 1
puts "Found datfiles (child) whose sets will be placed inside another datafile (father) folder:"
puts "father: #{filename}"
found.each {|f| puts "child: #{File.basename f}"}
#byebug
FileUtils.mv dat, dir.first # mv needs to receive either 2 strings or 2 arrays
puts "Moved datfile (to #{dir.first}):"
puts dat
puts "-------------------------"
end
end
puts "-------------------------"
puts "Moved #{duplicated+split} outdated datfiles, of these:"
puts "\t* due to direct updates: #{duplicated}"
puts "\t* due to category reorganizations: #{split}"