Foggy Bottoms has asked for the wisdom of the Perl Monks concerning the following question:

Monks of far and wide, bonjour and good day !

      I'm currently working on a Windows-based database containing references to Office documents in all my drives (eg c:\ and d:\). However, this database needs to be updated, should a file be created, modified or deleted.
      Instead of scanning the hard drive daily I thought I could have a look at folder properties and see whether its timestamp has changed since the last time I've updated the database. It'd spare me a lot of useless scanning.
      Does anyone know how to access folder properties ? Is it an OLE method ?

       Thanks monks for your help... FB
Heureux qui, comme Ulysse, a fait un beau voyage
Ou comme celui-là qui conquit la Toison,
Et puis est retourné plein d'usage et raison,
Vivre entre ses parents le reste de son âge!

J. du Bellay, poëte angevin

Replies are listed 'Best First'.
Re: folder properties / modified date
by snadra (Scribe) on Jul 21, 2003 at 10:42 UTC
    You can use the function stat for getting informations about a file:
    my @info = stat($file); my $last_access = $info[8]; my $last_modification = $info[9];
    more information about stat(): perldoc -f stat

    You can also use the file test operators for that.
    # Age of file in days since modification: -M $file # Age of file in day since last access: -A $file # Age of file in days since inode change: -C $file
    For more information read perldoc perlop
Re: folder properties / modified date
by wufnik (Friar) on Jul 21, 2003 at 12:53 UTC
    bonjour Foggy Bottoms;

    Perl for system administration, an extraordinarily handy book (David N. Blank-Edelman), gives the following as solutions to your problem: it allows you to watch in real time, if that is your penchant:

    use Win32::AdvNotify; $aobj = new Win32::AdvNotify(); $thread = $aobj->StartThread(Directory => 'C:\monks', Filter => All, WatchSubtree => 0) or die "monkwatcher cannot start\n";


    Filter Parameters can be more focused: ie changes in one of

    FILE_NAME

    DIR_NAME

    ATTRIBUTES

    SIZE

    LAST_WRITE

    CREATION

    SECURITY

    if all you are looking for is whether the directory size has changed, you can use the following:

    use Win32::DirSize; chomp(my $dir = shift || <DATA>); my @dirstats; push @dirstats,sprintf("%-40s%-10s%-10s%-10s\n", "Directory", "Size", "FileCount", "DirCount"); if (dir_size($dir, my $dirstat) == DS_RESULT_OK){ my $size = best_convert(my $unit, $dirstat->{HighSize}, $dirstat->{LowSize}); my $filecount = $dirstat->{FileCount}; my $dircount = $dirstat->{DirCount}; push @dirstats, sprintf("%-40s%-10s%-10s%-10s\n", $dir, sprintf("%8.4f", $size) . $unit, $filecount, $dircount); } push @dirstats, undef; map { print } grep { defined } @dirstats; __DATA__ \\captains\nemo$\nautilus
    hope that helps,

    wufnik

    -- in the world of the mules there are no rules --
      Thanks for your help - your node seems pretty comprehensive and I'll have a shot at it.
      I also tried snadra's idea which works fine on my NT4 system but doesn't work fine on Win2k. Any idea why ? Here's my bit of code...
      ####################### PRAGMA ####################### use strict; ####################### IMPORT ####################### use file::stat; my $file = "d:\\brossad\\"; # your own folder name here my @info = stat($file); my $last_access = $info[8]; my $last_modification = $info[9]; if (@info) { #print $last_access."\n"; my @time = (localtime($last_access)); $time[4]++; $time[5] += 1900; print ("time : $time[2]:$time[1]:$time[0] "."- $time[3]/$time[4]/$t +ime[5].\n"); } else { print "oops\n"; }
      It returns :
      time : 14:4:12 - 21/7/2003 on my machine
      , and
      time :  0:0:0  - 21/7/2003 on a Win2k machine... :(

      I checked the epoch time stamps (just to make sure the conversion bit wasn't the failing part) and the stamps are wrong under Win2k.
        I think I found out why time didn't come out for the
        last accessed
        property : it just doesn't exist in the Win98/2k folder properties - only the date is there. Oh well (sighs heavily).
        Thanks for your help.
Re: folder properties / modified date
by WhiteBird (Hermit) on Jul 21, 2003 at 12:52 UTC
    You might look into Win32::ChangeNotify, which monitors directory changes. There's a LAST_WRITE flag that can be used to monitor the last time that a file or directory was written to or modified. I have never used this extension myself, but I offer it out as a possibility, hoping this might be helpful.