#!/usr/bin/perl -w # # $Id: get_software_version.pl,v 1.1 2005/10/23 22:36:46 jmates Exp $ # # Copyright (c) 2005, Alex Dioso. # All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ##################################################################### # # Get the highest version of all software that matches the argument. # Becareful because there could be different software that has the # similar strings in the name like Mozilla, Mozilla Firefox, Mozilla # Thunderbird. # # Added option, if you specifiy a version number as the last argument # this script will compare that with the installed version. If the given # version is higher then return 1, else return 0. use strict; use Win32::TieRegistry ( Delimiter => "/" ); usage() unless ( ( scalar @ARGV ) == 1 ); my $software = shift; my $version = 0; my $Uninstall = $Registry->{ "LMachine/SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall/"}; for my $key ( keys %{$Uninstall} ) { if ( exists $Uninstall->{"$key/DisplayName"} and exists $Uninstall->{"$key/DisplayVersion"} ) { if ( $Uninstall->{"$key/DisplayName"} =~ m/$software/ ) { my $temp_ver; ($temp_ver) = ( $Uninstall->{"$key/DisplayVersion"} =~ m/([0-9.]+)/ ); $version = get_higher_version( $version, $temp_ver ); } } } print "$version\n"; sub get_higher_version { my @first = split /\./, shift; my @second = split /\./, shift; # We want the smaller array so we don't run beyond # if the arrays are even up to the last element of the smaller array # then the larger array is the newer version my $size = scalar @first < scalar @second ? scalar @first : scalar @second; for ( 0 .. $size - 1 ) { if ( $first[$_] > $second[$_] ) { return join ".", @first; } elsif ( $first[$_] > $second[$_] ) { return join ".", @second; } } # If we get here then the arrays are equal up to the smaller array so the # larger array must be the higher version return join ".", ( scalar @first > scalar @second ? @first : @second ); } sub usage { print <