General
cancel
Showing results for 
Search instead for 
Did you mean: 

Use Perl script to sort the file list by date in ftp and delete the old files

sammak
Occasional Visitor

Use Perl script to sort the file list by date in ftp and delete the old files

My project is using Perl script. I need to ftp to a server and sort the file list by date. Then the script will delete the old files (e.g. only retain the newest file and delete other old files in the folder).

I used the Net::Ftp and tried the ftp->ls. But it could not sort the files by date.

Can anyone help ?
4 REPLIES
Steven E. Protter
Exalted Contributor

Re: Use Perl script to sort the file list by date in ftp and delete the old files

Shalom,

Sounds like a project from a perl scripting class.

I like perl but would not use it for this.

find /path_to_apache_logs -name "access_log.*" -mtime 1 -exec rm {} \;


That is an example telling find to delete all apache logs more than a day old and delete them.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Use Perl script to sort the file list by date in ftp and delete the old files

Hi:

"My project..."

That sounds like homework. That said, if you use 'ftp->ls' method you get filenames. If you use 'ftp->dir' you get the equivalent of a long listing from 'ls'. You still need to write code to sort by date.

I might use 'ftp->ls' to collect the filenames in an hash with the filename as a key while calling 'stat()' on each file and store it's 'mtime' as the hash data. From there, you can do as you please.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Use Perl script to sort the file list by date in ftp and delete the old files

Hi (again):

Oops, I keep forgetting that you want to use 'ftp' to collect your file list. In that case, use the 'ftp->dir' method to return long listing of files (together with their date information). Since you can't stat() in this case, parse-out the date fields and convert them to epoch seconds for sorting and comparison purposes. Once you have reduced your list to the files you want to delete, run another FTP session to issue your removal.

I offer all this in the abstract assuming that you "must" use FTP to encapsulate your objective.

Regards!

...JRF...
sammak
Occasional Visitor

Re: Use Perl script to sort the file list by date in ftp and delete the old files

SEP and JRF, thanks for your advice. Actually, it is not an assignment. It is the system in my company. The system is under maintenance stage and written in Perl. So I need to use Perl function to do so.

Following is my code:
my $ftp = Net::FTP->new("$ftp_server",Timeout => 9000,Debug => 1);

$ftp->login("$ftp_login_name","$ftp_password") or die "$_: cannot login:".$ftp->message;

$ftp->cwd("$path");

my @fileListArray = $ftp->dir("LISTING*");
chomp(@fileListArray);

my $fileCount = @fileListArray;

if($fileCount > 0) {
while($i < $fileCount-$amountToKeep) {
my @tmp_x = split /\s+/,$fileListArray[$i];
my $current_file = $tmp_x[$#tmp_x];
system("del $path\\$fileListArray[$i]");
$i++;
}
}

$ftp->quit;

And is now trying to work the part for sorting the files by the dates.