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


hi all
I am doing a task to ftp to a server and pick up a file which is one day older than the current date
I am using the perl Net:FTP module

files are in the format yearmonthday.dat e.g. 20030601.dat (Ist of june)
my @current = `date`; ($dnr,$month,$date,$tnr,$rr,$year)= split(/\s+/,$current[0]); chomp $year;chomp $date;chomp $month; $date-=1; my %month_conv=("Jan"=>"01","Feb"=>"02","Mar"=>"03","Apr"=>"04","May"= +>"05","Jun"=>"06","Jul"=>"07","Aug" =>"08","Sep"=>"09","Oct"=>"10","Nov"=>"11","Dec"=>"12",); $pingmonth=$month_conv{$month}; $filename="$year$pingmonth$date.dat"; print "getting $filename\n"; $dir='/log_recv/telecom/DATA'; use Net::FTP; $ftp = Net::FTP->new("aaa.com")|| die("Could not connect: $@\n +"); $ftp->login("xxx","yyy")||die("Wrong username or password "); $ftp->binary(); $ftp->cwd("$dir")||die("Could not change path "); #@aa=$ftp->ls("$dir"); $ftp->get("$filename")|| die("File not found $filename\n"); $ftp->quit;

i would like to know the best method for handling end of the month conditions. e.g. on the 1st of june my script would fail
I had thought of
run ls -t take the output in an array and pick up arr1(second field as we need a one day older file)<how is this done using Net:FTP
convert to epoch seconds minus 86400 convert back and get .
anyideas on a efficient and reliable way
thanks

Replies are listed 'Best First'.
Re: FTP and get file date manipulation problem
by Enlil (Parson) on May 30, 2003 at 05:13 UTC
    i would like to know the best method for handling end of the month conditions

    I would look at Date::Calc on CPAN for dealing with date math. (there is probably something in DateTime as well, but as of yet i am not familiar with those modules). Here is some code to work off of:

    use strict; use warnings; use Date::Calc qw/Add_Delta_Days/; my ($mday,$mon,$year) = (localtime)[3..5]; my ($new_year,$new_month,$new_day) = Add_Delta_Days($year + 1900,$mon+ +1,$mday,-1); my $file_name = sprintf("%04d%02d%02d.dat", $new_year,$new_month,$new_ +day); print "Getting $file_name\n";
    Anyhow take a look at the Date::Calc documentation.

    -enlil

Re: FTP and get file date manipulation problem
by jkenneth (Pilgrim) on May 30, 2003 at 15:48 UTC
    First thing, don't use `date` to get the information you need.
    #!/usr/local/bin/perl @day = localtime(time() + 86400); $stamp = sprintf("%4d%02d%02d", $day[5] + 1900, $day[4] + 1, $day[3]); print $stamp;
    localtime() and Time::Local are useful functions to convert times between formats. sprintf() can be used to pad variables.

    One thing to note is that locatime returns years as number since 1900 and months are an array that starts at 0. Using this you don't have to worry about month/year changes since localtime takes care of it for you.

    JK