xawk.com - technical wisdom served up in little slices xawk.com -- Tags -- Ruby
 
xawk.com -- Tags -- Ruby -- Create Checkpoints using Ruby and MD5

Create Checkpoints using Ruby and MD5

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.

View Comments (0)

Tags: Ruby

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


 

Tag: Ruby
Using Ruby to fix id3 tags on mp3 files
Copy all files in a directory tree to a common destination.
Check for File Changes using Ruby and MD5
Create Checkpoints using Ruby and MD5

All Tags
Apple
Bash
Business Tips
Css
Delphi
Firefox
Gmail
Html
MySql
Php
Productivity Tips
Ruby
Ubuntu
Website Tips
Windows XP

Search