//Search through a directory looking for modified files - uses one-way hash function. Run once to initalize files, then run at a later date for analysis.
require 'digest/md5'
oldfile_hash = Hash.new()
newfile_hash = Hash.new()
valid = /(.*)\s{5}(\w{32})/
dir_array = Array.new()
dir_array[0] = ARGV.shift or raise "Missing path to traverse"
file_report = "#{dir_array[0]}\\file_report.txt"
file_output = "#{dir_array[0]}\\changed.files"
oldfile_output = "#{dir_array[0]}\\old_changed.files"
if File.exists?(file_output)
File.rename( file_output, oldfile_output)
File.open(oldfile_output, 'r+b') do |infile|
while (old_line = valid.match(infile.gets))
oldfile_hash[old_line[1]] = old_line[2]
end
end
end
report = File.new(file_report, 'wb')
changed_files = File.new(file_output, 'wb')
begin
p = dir_array.shift
Dir.chdir(p)
Dir.foreach(p) do |filename|
next if filename == '.' or filename == '..'
unless File::directory?(filename)
file = File.open(filename, 'rb')
newfile_hash[filename] = Digest::MD5.new(File.open(filename, 'rb').read).hexdigest
file.close unless file.closed?
else
dir_array << p + "\\" + filename
end
end
end while !dir_array.empty?
newfile_hash.each do |file, md5|
changed_files.write "#{file} #{md5}\n"
end
newfile_hash.keys.select {|file| newfile_hash[file] == oldfile_hash[file] }.each do |file|
newfile_hash.delete(file)
oldfile_hash.delete(file)
end
newfile_hash.each do |file, md5|
report.write "#{oldfile_hash[file] ? "Changed" : "Added"} file: #{file} #{md5}\n"
oldfile_hash.delete(file)
end
oldfile_hash.each do |file, md5|
report.write "Deleted/Moved file: #{file} #{md5}\n"
end