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.

Side Note: This article was written in 2008 so some of the references may be out-of-date.

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.

By Brad Trupp © 2008