IEClearCache 1.0.10 Code review

Hey all,

I just started to learn Ruby, and having a lot of fun with it.

I wanted to get some ideas by letting my peers and ubergeeks review this
small program I wrote (with a lot of help from this newsgroup I might add) I
am just getting use to OOP on a whole, so please be nice, I am more that
willing to learn better ways of doing things.

Im doing something rather scary, clearing IE’s cache from underneath it, if
there is a better way please let me know, or if I am just wrong in attempting
to do so, also please tell me :slight_smile:

Thank you for your time,

Here is the code (license and all):

<–start paste–>

#IEClaerCache - Cleaer your IE Cache file(s) from the system.
#Copyright © 2003 Ray Slakinski

···

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#GNU License @ http://www.gnu.org/copyleft/gpl.htm

#IEClearCache version 1.0.5, Copyright © 2003 Ray Slakinski
#IEClearCache comes with ABSOLUTELY NO WARRANTY

#Requirements:

Without modification of Paths, this program will only work on

Windows 2000 or XP Systems.

#Requires Windows
require ‘Win32API’
#Requires Ruby/DL – http://www.ruby-lang.org/en/raa-list.rhtml?id=629
require ‘dl’

#Set version number
IEClearCacheVer = ‘1.0.10’

code by Hee-Sob Park - posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10006

module ClUtilFile
ALL_FILES = ‘*’

def ClUtilFile.delTree(rootDirName, pattern=ALL_FILES)

if (rootDirName == '/') or (rootDirName.empty?)
	raise 'Cannot delete root or empty?'
end

if !File.exists?(rootDirName)
	raise 'rootDirName does not exist'
end

# puts all sub-dirs and filenames in array.
# Sub-dirs will be breadth first search.
dirsAndFiles = Dir["#{rootDirName}/**/" + pattern]

# delete all files first
dirsAndFiles.each do | dirOrFileName |
if FileTest.file?(dirOrFileName)
	if block_given?
		File.delete(dirOrFileName) if yield dirOrFileName
		else
			if dirOrFileName == 'Content.IE5/index.dat'
			else
				File.delete(dirOrFileName)
			end
		end
	end
end

# go through array backward to delete dirs in proper order
dirsAndFiles.reverse_each do | dirOrFileName |
	if FileTest.directory?(dirOrFileName)
		Dir.delete(dirOrFileName) if Dir.empty?(dirOrFileName)
		end
	end
	Dir.delete(rootDirName) if Dir.empty?(rootDirName)
end

end

code by Hee-Sob Park - posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10006

class Dir
def Dir.empty?(dirname)
Dir.entries(dirname).length == 2
end
end

#Parts of this code found on comp.lang.ruby and was written by Shusaku
(tsyk@yk.rim.or.jp)
#http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&selm=20021031162633.FB0E.TSYK%40yk.rim.or.jp
class DetectAndKillIE
User32 = DL.dlopen(“user32”)
enum_windows = User32[‘EnumWindows’, ‘IPL’]
post_message = User32[‘PostMessage’, ‘ILILL’]
get_class_name = User32[‘GetClassName’, ‘ILpI’]
temp = "n"
name = "IEFrame"
buff = " " * 16
WM_CLOSE = 0x0010
TRUE = 1

puts "\nChecking to see if IE is currently running on your PC, please 

wait…"
enum_windows_proc = DL.callback(‘ILL’) {|hwnd,lparam|
r,rs = get_class_name.call(hwnd, buff, buff.size)
if /^#{name}$/ =~ rs[1].to_s
if temp.upcase != “Y” then
puts "\nIE currently is running on your machine, please stop all your IE
session(s)"
puts "Do you want to exit all running sessions of IE? (y/n)"
temp = gets
temp = temp.chomp!
if temp.upcase == “Y” then
puts “\nExiting all IE Sessions, please wait…”
end
end

		if temp.upcase == "Y" then
			r,rs = post_message.call(hwnd, WM_CLOSE, 0, 0)
			#make sure it has time to close
			sleep .5
		else
			puts "\nThank you for using IEClearCache " + IEClearCacheVer + "\n"
			exit
		end
	end
	TRUE
}
r,rs = enum_windows.call(enum_windows_proc, 0)
sleep 1
puts "Done!"

end

class ClearCache
#Setup the user from system environment
userName = ENV[‘USERNAME’]
#setup the path (win2k/xp specific)
path = 'C:\Documents and Settings\'
path = path + userName + '\Local Settings\Temporary Internet Files\'
Dir.chdir(path)
puts "\nClearing Cache Files…"
ClUtilFile.delTree(“Content.IE5”)
puts "Done!\n"
end

#Get username from system enviroment
userName = ENV[‘USERNAME’]
#check to see if IE is running, if so kill it (prompts user)
DetectAndKillIE
#Clear the browser cache
ClearCache

puts “\nThank you for using IEClearCache” + IEClearCacheVer + “\n”

<–end paste–>

Ray Slakinski