BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Tags -- Code
BradTrupp.com -- Tags -- Code -- Check for File Changes using Ruby and MD5 (Ruby)

Check for File Changes using Ruby and MD5 (Ruby)
(2008/08/20)

by Brad Trupp (c) 2008

In an earlier article Create checkpoints using Ruby and MD5, I wrote about using the MD5 function in Ruby to create an checkpoint for all files in a directory.

Now the next step in this process is to use that initial checkpoint file as a starting point to find all changes that have happened since.

Here is a simple Ruby script to compare current checksums against an initial checksum hash file.

This particular example shows the basics of processing all the files in a directory and all the subdirectories underneath. In this case, my subdirectory is "out" and I am only processing files with a ".html" extension.

Note that the md5 checksums are loaded into a hash, sorted, and compared to find what's new, what has been added, and what has been removed.

checkpoint_changes.rb

#!/usr/bin/ruby -w
require 'digest/md5'

md5FileHash = {}
oldFileHash = {}

#----------------------------------------------------------------------
# Pass 0 - Get file list
#----------------------------------------------------------------------

theFileList = [];
Dir['out/**/*.html'].each do |fnn|
theFileList << fnn.downcase;
end;

#----------------------------------------------------------------------
# Pass 1 - calc md5
#----------------------------------------------------------------------

theFileList.each do |f|
digest = Digest::MD5.hexdigest(File.read(f))
md5FileHash[f] = digest;
end;

#----------------------------------------------------------------------
# Pass 2 - read old array from file
#----------------------------------------------------------------------

File.open("sync.dat").each do |line|
line = line.chop;
md5x = line[0,32];
filx = line[32,255];
oldFileHash[filx] = md5x;
end;


#----------------------------------------------------------------------
# Pass 3a - look for additions
# ( preferred upload order is additions, changes, then deletes)
#----------------------------------------------------------------------

for ddd in md5FileHash.keys.sort do
md5old = oldFileHash[ddd];
md5new = md5FileHash[ddd];
if md5old.nil? then puts "++ #{ddd}"; end;
end;

#----------------------------------------------------------------------
# Pass 3b - look for changes ( preferred upload order)
#----------------------------------------------------------------------

for ddd in md5FileHash.keys.sort do
md5old = oldFileHash[ddd];
md5new = md5FileHash[ddd];
if md5new != md5old then if !md5old.nil? then puts "<> #{ddd}"; end; end;
end;

#----------------------------------------------------------------------
# Pass 5 - look for deletes
#----------------------------------------------------------------------

for ddd in oldFileHash.keys.sort do
md5old = oldFileHash[ddd];
md5new = md5FileHash[ddd];
if md5new.nil? then puts "-- #{ddd}"; end;
end;

puts "DIFF complete.";

Tags: Code

Share: Del.icio.us | Digg | Facebook | Google Bookmarks | Reddit | Technorati | Twitter | Windows Live | Yahoo! My Web

View Comments (0)


 

Tag: Code
A Little Bit of Automation for your Backups (2010/02/25)
A Simple CSV to iCalendar Conversion Utility written in Ruby (2009/06/29)
A Simple iCalendar to Task Coach Conversion Utility written in Ruby (2009/05/09)
Using Ruby to generate Social Bookmarks for your Web Pages (2008/11/23)
Using Ruby to fix id3 tags on mp3 files (Ruby) (2008/11/10)
Copy all files in a directory tree to a common destination. (Ruby) (2008/10/08)
Check for File Changes using Ruby and MD5 (Ruby) (2008/08/20)
Create Checkpoints using Ruby and MD5 (Ruby) (2008/07/30)
Bash - A Basic Greeting Program. (Bash) (2007/10/31)
Bash - An Introduction to Scripting (Bash) (2007/10/10)
UUMerge Command Line Text File Merge (Delphi) (2007/07/01)
Explorer-Style Fly-By Buttons (Delphi) (2006/01/14)
Handle-Free Checkboxes in a String Grid (Delphi) (2006/01/14)

All Tags
Business Tips (5)
Code (13)
Life Skills (1)
Music (2)
My 15 minutes (5)
Old Articles (39)
Photos (10)

Advertisement