Perl Code to Parse File Versions

In various automations,. systems engineers require to determine the version of file to perform a action based on the file version. Here is a excerpt code that checks for the version of a specified file.

CODE#1

 
#===============================================================================
# sub CheckFileVersion
#===============================================================================
sub CheckFileVersion {
    if (-e "$File") {
        my $cmd = "filever \"$File\"";
        my $output = `$cmd`;
        my @lines = split(/ /, $output);
        my $ver     = ($lines[7] eq '') ? $lines[8] : $lines[7];
        my @version = split(/\./, $ver);
        my $filever = $version[0];
        if ($filever) {
            print "INFO: $File major version is $filever\n";
        } else {
           print "ERROR: Failed to retrieve major version of $File\n";
        }
    } else {
        print "WARNING:  Specified file doesn't exist on this machine.\n";
    }

}

CODE#2

my $cmd = "filever.exe \"$FILE\"";
my $file_version     = `$cmd 2>&1`;
if ($file_version    =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
    $file_version    = "$1.$2.$3.$4";
} else {
    print "ERROR: Failed to determine $FILE version\n";
}
Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.