What was created or modified yesterday?

How To

Ever want to have a list of what files were created or updated in the last 24 hours? Or maybe you run a big old-fashioned website and you want to have a "What's New" page that actually shows what's new. I wrote this Perl script to keep track of what files were modified on a (Microsoft) web server. I was having some trouble with someone who would edit files without telling me. The script builds a list of files and sends this list to me via email. It is set to run daily at midnight.

#!C:\perl\bin\perl.exe
use NTsendmail;
use Time::localtime;
#######################################################################
# modfilesweep_website.pl
#
# Author:   Steve DeGraeve
# Created : August 30th, 2001
# Modified: December 14th, 2001
#
####################################################################### 
$root                = "D:\\inetpub\\wwwroot\\mywebsite";
$baseurl             = 'http://www\.mywebsite\.com';
@files               = ();
@dirs                = ();
$numdays             = 1;
$currenttime         = time();
$twentyfourhoursago  = time() - ($numdays*86400);
$log                 = "";
%files               = ();

$emailfrom           = 'youremail@example.com';
$emailto             = 'youremail@example.com';
$emailsubject        = "Website Changes";
$smtpserver          = "smtp.example.com";

#######################################################################

push @dirs, $root;

#
# TRAVERSE DIRECTORIES
#
    foreach $d (@dirs){
        # OPEN ROOT DIRECTORY
        opendir DIR, $d || print "Error opening DIR $d\n";
        
        # GET DIRECTORIES IN LIST
        @alldirs = readdir DIR;
        foreach $dr (@alldirs){
            if (($dr ne ".") && ($dr ne "..") && (-d $d."\\".$dr)){
                push @dirs, $d."\\".$dr;
            }
        }
        close DIR;

        opendir DIR, $d || print "Error opening DIR $d\n";  
        # READ LIST OF FILES
        @allfiles = grep !/^\./, readdir DIR;
        foreach $f (@allfiles){
            $filename = $d . "\\" . $f;
            ($dev,$ino,$md,$nlnk,$uid,$gid,$rd,$sz,$at,$mtime,$ct,$b,$bs) = stat $filename;
            if ($mtime > $twentyfourhoursago && $mtime < $currenttime){
                $files{$filename} = $mtime;
            }
        }
        closedir DIR;
    }

#
# SORT MODIFIED FILES BY NAME AND COMPILE THE LOG OF THEM
#
    @keys = sort keys %files;
    foreach $key (@keys){
        $tm = localtime($files{$key});
        $month   = $tm->mon+1;
        $day     = $tm->mday;
        $hours   = $tm->hour;
        $minutes = $tm->min;
        $seconds = $tm->sec;
        $temp = sprintf("%02d/%02d %02d:%02d:%02d",$month,$day,$hours,$minutes,$seconds);
        $key =~ s|$root|$baseurl|ig;
        $key =~ s|\\|\/|g;
        $log .= "$key - $temp\n";
    }

#
# COMPOSE EMAIL
#
    $ENV{"NTsendmail"} = $smtpserver;
    $mail = new NTsendmail;
    $from = $emailfrom;
    $to = $emailto;
    $subject = $emailsubject;
        $tm = localtime(time());
        $month   = $tm->mon+1;
        $day     = $tm->mday;
        $hours   = $tm->hour;
        $minutes = $tm->min;
        $seconds = $tm->sec;
        $temp = sprintf("%02d/%02d %02d:%02d:%02d",$month,$day,$hours,$minutes,$seconds);
    $message = "HELLO!\nHere are the pages that have been edited in the last ";
    $message .= $numdays * 24 . " hours (since " . $temp . "):\n\n";
    $message .= $log . "\n";
    print $message;

#
# SEND EMAIL IF THE LOG IS NOT EMPTY
#
    if ($log ne ""){
        $mail->send($from, $to, $subject, $message);
    }

#    
# DONE.
#