BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Tags -- Code
BradTrupp.com -- Tags -- Code -- Create Checkpoints using Ruby and MD5 (Ruby)

Create Checkpoints using Ruby and MD5 (Ruby)
(2008/07/30)

By Brad Trupp (c) 2008

One of the things I use Ruby for is my "home-grown" CMS or content management system.

To make things easy, I use a hash file of md5 checksums to be able to tell what files have changed in my output directory so I can tell what to upload and what to delete off the web server.

Here is a simple Ruby script to create 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.

checkpoint_create.rb

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

md5FileHash = {}

#----------------------------------------------------------------------
# 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 - write array to file
#----------------------------------------------------------------------
syncfile = File.new("sync.dat", "w+");
for ddd in md5FileHash.keys.sort do
syncfile.puts "#{md5FileHash[ddd]}#{ddd}";
end;
syncfile.close;

puts "Sync.dat created.";

The script to identify differences will be found in a later article.

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