Friday, May 18, 2007

file date comparison in perl

This is a little sample code that shows how to compare the last-modified time for one file with that of another in Perl:

#!/usr/bin/perl
use strict;
use warnings;

my $ofile = "/home/wereldmuis/oldfile";
my $nfile = "/home/wereldmuis/newfile";
open(FO, $ofile);
open(FN, $nfile);

my @info = stat FO;
my $oftime = $info[8]; # file modification time in epoch seconds

@info = stat FN;
my $nftime = $info[8];

my $current = time(); # current time in epoch seconds, just an example

if ($nftime < $oftime){
 print "$nfile is older than $ofile\n";
} else {
 print "$ofile is older than $nfile\n";
}

Output is "/home/wereldmuis/oldfile is older than /home/wereldmuis/newfile".

Labels: ,