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

Hello, I am new to perl, but love it so far. I know this is a very simple problem, but I thought I would ask for some assistance.

I need some help trying to download files from an FTP site based on their timestamp or date format(MMDDYYYY), then send an email if the download was successful or not.

So far I setup Net::FTP to successfully download the file if I specify the exact filename. I have not been able to successfully setup $ftp->_MDTM($file) to download any files that have a timestamp with today's date. I would also like to setup Net::SMTP to send an email stating if the file transfer was successful or not.

Any help or information would help. The following is my current script(not complete by any means).

#!/usr/bin/perl # pegasus_ftp_transfer use Net::FTP; use Time::Format qw(%time %strftime %manip); $ctime="$time{'hh:mm'}"; $cdate=$time{mmddyy}; $file_month=$time{'mmddyy'}; $filename= "property_odd04012004.gz"; $ftp_log="pegasus_ftp_transfer.log"; $down_dir = "c:"; $ftp = Net::FTP->new("host", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("user","pass") or die "Cannot login ", $ftp->message; $ftp->ascii; $ftp->cwd("/pegasus") or die "Cannot change working directory ", $ftp->message; $ftp->mdtm ($filename) or die "get failed ", $ftp->message; $ftp->quit; 'mv $filename > $down_dir';

Edit by castaway, added code tags

Replies are listed 'Best First'.
Re: FTP pattern matching and email notify
by Thelonius (Priest) on May 18, 2004 at 13:57 UTC
    First, you need to have "use warnings;" and "use strict;" on all your programs. This will save you a lot of grief.

    Before you do somthink like $cftp->mdtm($filename) you have to actually have a filename. E.g.

    for my $filename ($ftp->ls) { my $filetime = $ftp->mdtm($filename) or die "mdtm failed ", $ftp->message; # here check if filetime is in your range }
Re: FTP pattern matching and email notify
by muntfish (Chaplain) on May 18, 2004 at 13:52 UTC

    I'm not 100% sure what you're trying to do. Do you want to get the files by name, or by date?

    At any rate, you're on the right line with that code, but you need to check what $ftp->mdtm is returning. You could try something like this (after the cwd):

    my @d = $ftp->ls(); for (@d) { my $mdtm = strftime("%Y/%m/%d %H:%M:%S", localtime($ftp->mdtm( +$_))); print "filename = $_\nmdtm = $mdtm\n"; # check file is newer than whatever criteria you have if ($mdtm ge "whatever") { $ftp->get($_); } }

    Sorry if I've misunderstood your question. Hope this helps in some way...

    Update: fixed the strftime format string.

Re: FTP pattern matching and email notify
by McMahon (Chaplain) on May 18, 2004 at 16:58 UTC
    I think that instead of "mdtm", you'll want to use "ls" and then parse the output, for instance like so:

    my @files = $ftp->ls(); foreach my $file(@files) { $ftp->get($file) or die "get failed", $ftp->message; }


    Hope that helps...
Re: FTP pattern matching and email notify
by eserte (Deacon) on May 18, 2004 at 14:13 UTC
    I suspect that there may be FTP servers which do not support the MDTM command.