-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCRCToFile.rb
63 lines (50 loc) · 1.18 KB
/
addCRCToFile.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
#!/usr/bin/env ruby
# encoding: UTF-8
##
# Appends a CRC32 in the given file name
#
# e.g.: my.sample.file.mpg -> my.sample.file.[1f2e3d4c].mpg
#
# @author rdoi
# @created 12/2013
require 'zlib'
include Zlib
require 'term/ansicolor'
include Term::ANSIColor
def getCRC(fname)
puts fname
f = nil
File.open(fname, 'rb') { |h| f= h.read } ; nil
return crc32(f,0).to_s(16)
end
def addCRC(fname)
if (fname.nil? || !File.exists?(fname))
puts "File not found: #{ARGV[0]}"
else
crc= getCRC(fname)
if (fcrc= /[0-9a-fA-F]{8}/.match(fname))
if (fcrc[0].downcase == crc)
puts "Valid #{crc}: #{fname}".green.bold
else
puts "INVALID #{crc}: #{fname}".red.bold
end
else
fext=File.extname(fname)
fbase=File.basename(fname,fext)
fpath=File.dirname(fname)
newName=File.join(fpath,"#{fbase}.[#{crc}]#{fext}")
puts "#{fname} -> #{newName}" #.blue.bold
File.rename(fname,newName)
end
end
end
if (__FILE__ == $0)
if (ARGV.length < 1)
puts "Appends a CRC32 in the given file name\n\n"
puts "e.g: my.sample.file.mpg -> my.sample.file.[1f2e3d4c].mpg\n\n"
puts "Use #{File.basename(__FILE__,'.rb')} [file]"
exit
else
addCRC(ARGV[0])
end
end