CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
get latest file using Net::FTP

 



yahhodeol
Novice

Mar 31, 2010, 5:30 PM

Post #1 of 8 (798 views)
get latest file using Net::FTP Can't Post

Hi

Everyday a new file is created on remote linux box in the format:

filename_2010_03_10.txt
.
.
filename_2010_03_31.txt and so on........

I need to ftp latest file with filename_*.txt format because there are files other than above format too in same folder which I don't need.

Right now I am getting all files(filename_*.txt). As the files size is increasing and we have slow internet @ remote end. I should get only single new file.

Can anyone suggest perl script using (Net::FTP;)?


Thanks:
-Depp

(This post was edited by yahhodeol on Mar 31, 2010, 5:31 PM)


7stud
Enthusiast

Mar 31, 2010, 10:16 PM

Post #2 of 8 (788 views)
Re: [yahhodeol] get latest file using Net::FTP [In reply to] Can't Post


Code
use strict; 
use warnings;
use 5.010;

use Date::Manip::Date;

my %filename_for = (
_2010_03_10 => 'filename_2010_03_10.txt',
_2010_03_31 => 'filename_2010_03_31.txt',
_2010_02_22 => 'filename_2010_02_22.txt',
);

my @sorted_keys = sort {
my $date_a = Date::Manip::Date->new;
my $date_b = Date::Manip::Date->new;
my $format = "_%Y_%m_%d";

my $error_a = $date_a->parse_format($format, $a);
my $error_b = $date_b->parse_format($format, $b);

if ($error_a == 0 and $error_b == 0) { #then successfully parsed dates
date_a->cmp($date_b);
}
else {
die "Couldn't parse dates";
}
} keys %filename_for;


for my $date (@sorted_keys) {
say $filename_for{$date};
}

--output:--
filename_2010_02_22.txt
filename_2010_03_10.txt
filename_2010_03_31.txt



(This post was edited by 7stud on Apr 1, 2010, 7:02 AM)


yahhodeol
Novice

Apr 1, 2010, 11:40 AM

Post #3 of 8 (776 views)
Re: [7stud] get latest file using Net::FTP [In reply to] Can't Post

Thanks 7stud.

One question. Isn't In this script I have to manually define name of all files?


7stud
Enthusiast

Apr 1, 2010, 12:41 PM

Post #4 of 8 (774 views)
Re: [yahhodeol] get latest file using Net::FTP [In reply to] Can't Post

Nope. You first have to write the code that will construct that hash. You need to get the ls output, then construct the hash from that array.


(This post was edited by 7stud on Apr 1, 2010, 12:42 PM)


yahhodeol
Novice

Apr 1, 2010, 2:27 PM

Post #5 of 8 (764 views)
Re: [7stud] get latest file using Net::FTP [In reply to] Can't Post

I am getting file list using following script.

#!/usr/bin/perl
use Net::FTP;

my $host="www.com";
my $directory="/path/to/files";

$ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";

$ftp->login("username","password") or $newerr=1;
print "Getting file list";
push @ERRORS, "Can't login to $host: $!\n" if $newerr;
$ftp->quit if $newerr;
myerr() if $newerr;
print "Logged in\n";

$ftp->cwd($directory) or $newerr=1;
push @ERRORS, "Can't cd $!\n" if $newerr;
myerr() if $newerr;
$ftp->quit if $newerr;

@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list $!\n" if $newerr;
myerr() if $newerr;
print "Got file list\n";
foreach(@files) {
print "$_\n";
}
$ftp->quit;


sub myerr {
print "Error: \n";
print @ERRORS;
exit 0;
}




So, now shall I change it to:-


for each(@files)
{my @sorted_keys = sort {
my $date_a = Date::Manip::Date->new;
my $date_b = Date::Manip::Date->new;
my $format = "_%Y_%m_%d";

my $error_a = $date_a->parse_format($format, $a);
my $error_b = $date_b->parse_format($format, $b);

if ($error_a == 0 and $error_b == 0) { #then successfully parsed dates
date_a->cmp($date_b);
}
else {
die "Couldn't parse dates";
}
}
}


????
and then again put ftp in loop to get file, I guess.


7stud
Enthusiast

Apr 1, 2010, 6:54 PM

Post #6 of 8 (757 views)
Re: [yahhodeol] get latest file using Net::FTP [In reply to] Can't Post

Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.


yahhodeol
Novice

Apr 2, 2010, 3:52 PM

Post #7 of 8 (746 views)
Re: [7stud] get latest file using Net::FTP [In reply to] Can't Post

I am looking to match string(date part in file name) with output from localtime() function.

This is for testing that i have printed (print "$stra $strb $strc\n";) instead of doing ftp.

I am getting output($stra $strb $strc) of all lines even it's
in the if condition.

Below is the updated script.

#!/usr/bin/perl
use Net::FTP;
use Date::Format;


my $host="www.com";
my $directory="/path/to/files";

$ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";

$ftp->login("username","password") or $newerr=1;
print "Getting file list";
push @ERRORS, "Can't login to $host: $!\n" if $newerr;
$ftp->quit if $newerr;
myerr() if $newerr;
print "Logged in\n";

$ftp->cwd($directory) or $newerr=1;
push @ERRORS, "Can't cd $!\n" if $newerr;
myerr() if $newerr;
$ftp->quit if $newerr;

@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list $!\n" if $newerr;
myerr() if $newerr;
print "Got file list\n";

$ftp->quit;

my $timing = time2str("%Y %m %d", time);
print "$timing\n";

foreach(@files) {
$_=substr($_,41);
s/ */ /g;
s/^ *//g;
chomp;
@stuff=split / /;
my $stra = substr($stuff[4],13,4);
my $strb = substr($stuff[4],18,2);
my $strc = substr($stuff[4],21,2);
if(($stra == $timing[0]) && ($strb == $timing[1]) && ($strc == $timing[2]))
{
print "$stra $strb $strc\n";
}
}

sub myerr {
print "Error: \n";
print @ERRORS;
exit 0;
}


Code
 
Now, if i remove if condition and type only,
print "$stra $strb $strc\n";
print "$timing[0] $timing[1] $timing[2]";

I am getting right output: attached file(ftp_test.doc)

I am making some mistake while comparing them. I guess
data type.

Please correct me.


Thanks

(This post was edited by yahhodeol on Apr 3, 2010, 1:32 PM)
Attachments: ftp_test.doc (43.5 KB)


7stud
Enthusiast

Apr 2, 2010, 5:26 PM

Post #8 of 8 (743 views)
Re: [yahhodeol] get latest file using Net::FTP [In reply to] Can't Post

Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives