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

Hi Monks, I have a FTP script that does the ftp from the server. The server conatin plenty of zip files, in those files i require few ie latest ones, and for the single day there will be multiple files in this format
arident16_Jul282009_2358.zip arident16_Jul282009_2320.zip arident16_Jul272009_1619.zip
I want o campare if it is todays files and time is 2358 then only it should be allowed to be ftp'ed else let it be in the server. I am pasting my code here....
#!/usr/bin/perl use Net::FTP; use Archive::Zip; use IO::File ; print "Retrieving file from abc.com...\n"; $loginip='12.34.56.78'; $loginid='login'; $loginpaswd='password'; ($sec,$min,$hour,$mday,$mon,$year)=(localtime(time))[0,1,2,3,4,5]; $time_stamp= "_" . (1900+$year) . "_" . ($mon+1) . "_" . ($mday) . "_" + . $hour . "_" . $min . "_". $sec; printf "time stamp = $time_stamp\n"; unless(-d "TFLogs") { mkdir("TFLogs"); } if( -d "TFLogs") { chdir("TFLogs"); } $ftp = Net::FTP->new(($loginip), Debug => 0) or die "Cannot connect to abc.com: $@ \n"; $ftp->login($loginid,$loginpaswd) or die "Cannot login ", $ftp->message; $source_dir="nt12/logs/"; $ftp->cwd($source_dir) or die "Cannot change working directory ", $ftp->message; $ftp->binary || die "Unable to set mode to binary. ", $ftp->message; @list=$ftp->ls(); printf "list = \n"; print @list; foreach $file (@list) { <quote> i want to fit in my code here to check for the latest file and time. </quote> $ftp->get($file) or die "get failed ", $ftp->message; my $zipname = $file; my $destinationDirectory = 'C:\Perl Script\TFLogs'; my $zip = Archive::Zip->new($zipname); foreach my $member ($zip->members) { next if $member->isDirectory; (my $extractName = $member->fileName) =~ s{.*/}{}; $member->extractToFileNamed( "$destinationDirectory/$extractName"); } } $ftp->quit;
Thanks NT

Replies are listed 'Best First'.
Re: date in required format
by FalseVinylShrub (Chaplain) on Jul 29, 2009 at 16:03 UTC

    Hi

    So, you are running through the files on the server and you want to fetch only those whose names end with _Jul282009_2358.zip (for the current date).

    One way to get the date in that format is using POSIX::strftime. Here is a quick test:

    use strict; use warnings; use POSIX qw(strftime); print strftime("arident16_%b%e%Y_2358.zip\n", gmtime); output: arident16_Jul292009_2358.zip

    One potential problem with strftime is that the output depends on your locale settings. In other words, the abbreviation Jul may come out differently (in a different language, for example). If so, you could continue constructing the timestamp manually, as you attempted at the top of your code. I will continue with strftime for now, because it makes the code smaller.

    So to insert into your code:

    ### at the top of the file: use POSIX qw(strftime); ### ... ### just before your foreach loop: my $files_to_match = strftime("%b%e%Y_2358.zip", gmtime); foreach $file (@list) { # N.B. I am assuming $file contains the filename here. # skip files that don't end with $files_to_match # N.B. could use regex here, not sure what's faster. next unless substr($file, 0 - length($files_to_match)) eq $files_to_match; ### Then what you did already. I didn't check that. } ### quit, etc...

    I have tested the above in a very basic way...

    I didn't check the rest of your code because it seems like you are just asking how to skip files that don't match. I did notice that you are using a mixture of print and printf. In Perl, you generally use print most of the time and only need printf or sprintf when using the formatting strings (%0.2d, etc.). You should think about using warnings and strict (as in my strftime example script, above).

    Hope this helps.

      You do the math (subtract a day). Date math can be very complicated (daylight savings, etc), which is why you should use DateTime.
        Any sample example of using DateTime. Thanks NT
        Hi Monks, I got it working this way.
        my($var1 ,$var2 ,$var3 ,$var4 ,$var5) = split (/ /, scalar (localtime( +time - 86400))); my $dateformat = "$var2$var3$var5";
        Thanks NT
      Thanks alot for your valuebale information. I have one more query and ie -- how to get the previous day date in the format i asked. Thanks NT
Re: date in required format
by psini (Deacon) on Jul 29, 2009 at 15:33 UTC

    I'm sorry, but I couldn't find a question in your post.

    Update: Forget it. I found it, in the middle of the code.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."