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

Hi Monks

I want to scan all the files in a directory. Find the ones that have been modified since I last run the program.

Present them on a web page.

Select a subset of them. Save the subset to a database.

Copy the subset to across to another directory.

What CPAN modules could be useful for this?

Replies are listed 'Best First'.
Re: Checking for files changed
by andyford (Curate) on Dec 15, 2006 at 16:18 UTC
    Depending on your Perl version, you can use things that come with Perl for most of this.

    If you find a module you want to use, just

    perl -e 'use <insert_Name_of_Module>'
    to find out if you already have it.

    To scan the files in a particular directory, just use the builtins readdir and opendir. Then, to get their modification time use stat. If you need to recurse into subdirectories, you might need File::Find.

    To create a webpage, just print you tags and data to a new HTML file. If you want to trigger the scan from a web page to a make a web application, then use CGI.

    Most people use the DBI module to access databases.

    File::Copy is an obvious choice for copying files.

    non-Perl: Andy Ford

Re: Checking for files changed
by Fletch (Bishop) on Dec 15, 2006 at 16:15 UTC
Re: Checking for files changed
by SFLEX (Chaplain) on Dec 15, 2006 at 16:36 UTC
    File::Stat is good for getting the info of the file (i.e. File Last modified date, last accessed date, date created, est.)
    and "if Perl 5.8 or higher then use File::stat.".

    Good Luck!

    Updated: Since your messing with Files you may want to also try Taint for some security.
Re: Checking for files changed
by shmem (Chancellor) on Dec 15, 2006 at 19:35 UTC
    I want to scan all the files in a directory. Find the ones that have been modified since I last run the program.

    opendir, readdir for reading a single directory, File::Find to read recursively. Calculate a checksum with e.g. Digest::MD5 and store filenames and checksum in a database, probably a self-contained like DBI::SQLite.

    Present them on a web page.
    Oodles of modules here. CGI, Template::Toolkit, HTML::Template, Mason to name a few.
    Select a subset of them. Save the subset to a database.
    JavaScript, and see above, and see above.
    Copy the subset to across to another directory.

    File::Copy

    TIMTOWDI - these are just the apples I can think of that hang lowest, and are easiest to pick.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Checking for files changed
by dwhite20899 (Friar) on Dec 17, 2006 at 21:42 UTC
    When I have a need like this, I think of find2perl. If you look at the man page for find, you see that there is a way to list files newer than a date or another file. Say you keep a file called 'lasttime' that you modify the last time yo run your check...

    find2perl /mydir -type f -newer lasttime -print > find-recent-files.pl

    gets you some perl that is close to what you want, and you can change the strings '/mydir' and 'lasttime' to command line input.

    #! /opt/local/bin/perl -w eval 'exec /opt/local/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; my $timefile = shift; my $rootdir = shift; (-e "$timefile") or die "$0 : last mod time file \"$timefile\" does no +t exist\n"; (-d "$rootdir") or die "$0 : directory \"$rootdir\" for search does no +t exist\n"; my $AGE_OFlasttime = -M "$timefile"; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, "$rootdir"); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ && (-M _ < $AGE_OFlasttime) && print("$name\n"); }