in reply to date in required format

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.

Replies are listed 'Best First'.
Re^2: date in required format
by Anonymous Monk on Jul 30, 2009 at 09:18 UTC
    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
Re^2: date in required format
by namishtiwari (Acolyte) on Jul 30, 2009 at 09:11 UTC
    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