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

The following lines of code are from my first perl script, which is to connect to a remote server and delete files of a certain type in a certain folder that are older than a certain time.

$ftp->mdtm($file) || die "cannot get mdtm\n";
my $modTime=$ftp->message;

I cannot get the modified time to be returned. Is it likely to be something as simple as the remote (linux)server not able to cope with mdtm? No errors are reported when I run it, just no modified time.
If anyone has any pointers I'll be very happy
Thanks
stewartski

Replies are listed 'Best First'.
Re: using $ftp->mdtm($file)
by Limbic~Region (Chancellor) on Feb 24, 2004 at 16:28 UTC
    stewartski,
    I was looking through the source code for Net::FTP thinking the || die piece of your code wasn't working because it returned "true but bad" response. When I couldn't find one (it returns undef if there is a problem) I noticed you are calling it in a void context - try this instead.
    my $mtime = $ftp->mdtm($file); die "cannot get mdtm" if ! $mtime;
    Cheers - L~R
      Thanks L~R, I took your advice, but with the same result. Below is the code for my entire foreach loop. I suspect the problem might be in the first line of the loop, although the first if statement works fine.

      foreach my $file ($ftp->ls()) {
      print "in the foreach loop at $file\n";
      if (!($file=~/.?\.stm/)) { print "$file is not an .stm type file\n"; next; }
      my $mtime = $ftp->mdtm($file);
      die "cannot get mdtm\n" if ! $mtime;
      print "modified time is $mtime\n";
      chomp($mtime);
      if (!($mtime=~/\d+/)) {
      print "skipping $file\n";
      next;
      }
      if ($mtime<$timeThen) {
      print "Removing $file: ";
      $connect->delete($file);
      print $ftp->message;
      }
      }

      This works in that any file that is not a .stm file shows up in the "not an .stm file" message. The program exits on the die "cannot get mdtm" line the first time an .stm file is found.
      Any advice gratefully received
      thanks
      stewartski
        stewartski,
        Perhaps you should take a look at PerlTidy as good indention really helps in readability and bug hunting. Since you have not shown all of the code, I do not know what $timeThen is so I will assume it is an epoch time stamp. You also do not show where $connect comes from? Are you using warnings and strictures? I have never used Net::FTP, but I would guess it should look something like this.
        my $stamp; # set to appropriate epoch time stamp my @files = $ftp->ls(); chomp( @files ); for my $file ( @files ) { print "Testing file : $file\n"; if ( $file !~ /\.stm$/ ) { print "Skipping : $file is not an .stm file\n"; next; } my $mtime = $ftp->mdtm($file); die "cannot get mdtm for $file\n" if ! $mtime; print "$file last modified : ", scalar localtime($mtime), "\n"; if ( $mtime < $stamp ) { print "Removing file : $file\n"; $ftp->delete($file); print $ftp->message, "\n"; } }
        Cheers - L~R
Re: using $ftp->mdtm($file)
by delirium (Chaplain) on Feb 24, 2004 at 18:25 UTC
    L~R's approach is correct: Feed $ftp->mdtm() right to a scalar.

    Also, don't forget to log off the server. Killing a process before sending 'QUIT^M' to the server is not nice.

Re: using $ftp->mdtm($file)
by crabbdean (Pilgrim) on Feb 24, 2004 at 18:31 UTC
    I've JUST tonight finished writing a program to do this type of task, although it doesn't connect via FTP, but can scan any visible drive. I designed it to scan our terabyte server at work and remove the compounding temp files on there. On its maiden voyage it remoeved some near 12Gb of temp files, about 400,000 files. I'm sure you can amend it to suit your needs.

    You can specify any number of files you wanted deleted based on regular expressions in external configuration files. You can also specify entire paths to be removed if you want. It'll test them to make sure they are all older than 7 days. Having these in external files means you can change what you want deleted without having to always be changing the source code.

    It'll also output a result of how much space was recovered, files scanned etc. etc.

    Its taken me a few months to write as in the process I found that Perl's File::Find module had a memory leak and required me to rewrite the core of the program after I'd completed it the first time. I've also written a Tech Spec for it to give you an overview to the code.

    If you want a copy I won't post it here because its some 600 lines long but email me and I'll send you a copy and the tech spec. You may be able to adapt it to your means and make it work with FTP. Or it may just give you some ideas. Considering I started it so long ago I'd probably write it differently now, I'm not overly happy with the code, but it works a treat and I can't be bothered spending any more time on it.

    Cheers,
    Dean
    crabbdean at yahoo.com.au