Confused...

Jas wrote:
I am not sure how I would be able to accomplish this, still fairly new to using Perl.

Objective: Connect to a remote machine and list all files matching a given pattern (i.e. 02242002-www.tar.gz, where 02242004 is the day of month). Delete all files that are older than 30 days from last file listed.

What does "last file listed" mean? Last updated, last by name, or last in sort order (as your dates do not sort properly, YYYYMMDD is a better format)?



Here is what I have thus far, and it works (not too shaby for a newbie).


#!/usr/bin/perl


# not too shabby except you are missing two keys... use strict; # always use warnings; # usually

use Net::FTP;

$ftp = Net::FTP->new("test-serv", Debug => 1)or die "Could establish conneciton to test-serv: $@";


Attention to detail is a good habit to get into, even when that means proper grammar and spelling in error conditions... "Could not" and "connection"...


$ftp->login("user", 'password')or die "Could not login ", $ftp->message;

foreach my $list(glob('*-www.tar.gz')) {
 $ftp->ls($list)or die "Could not list files ", $ftp->message; }


Your 'glob' is on the local machine and you are listing the specific filename on the remote server which must be a directory, and then throwing that value away (void context)??


foreach my $del(glob('


I assume the above should have been [snipped].


foreach my $file (glob('/path/to/*-www.tar.gz')) {
 $ftp->put($file) or die "Could not transfer files ", $ftp->message; }


Ok now we are putting a set of local files to a remote location.



$ftp->quit;

I suppose I need to look up how to check dates and then do some sort of eval() but I am not sure... any help or pointers to a good tutorial on this kinda thing is appreciated.
Jas



Back to your objective, assuming it is correct...


1. List files on remote machine
2. Step through list of files sorting into a list that we can then retrieve all but the 30 newest
3. Step through list of files needing deletion, deleting them from the remote source.


'eval' should be unnecessary.

Tell us what you really are doing :-)...

my @remote_list = $ftp->ls;

# massage @remote_list to only include those files we wish to delete
# perldoc -f split
# perldoc -f sort

foreach my $file (@remote_list) {
  $ftp->delete($file) or warn "Cannot delete remote file", $ftp->message;
}

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to