Scott, Deborah wrote:
> I need to sort files according to the date they were created. Is
> there a way to do this for files stored on an NT machine?
> 
> I have a script that reads files from a directory and then creates an
> HTML page listing the files. (Thanks to Dan Muey and Pete Emerson.)
> 
> But one directory contains files with the exact same title, except
> for the date (which is NOT written to sort correctly).
> Examples:
> "Daily Mailer 2103"(this is the Mailer for Feb 1, 2003), "Daily Mailer
> 112102"(this is the Mailer for November 21, 2002), "Daily Mailer
> 21103" (this is the Mailer for Feb 11, 2003), etc.
> 
> I'm having to manually create an HTML page every day, with the newest
> Mailer. Does anyone have a solution for an "automatic" script? If I
> could sort by date created on an NT machine, that would do it. (Or
> some sort of sorting subroutine could be written that sorts out the
> date, but I haven't figured that out yet.)
> 
> Thanks!!
> 
> Deborah

        You can use the stat function look at element 10 which is create time. Then 
you can sort down in descending sequence(newest to oldest dates).
        Here is a start using stat element 10:

my $MyDir = 'yourdirectorygoeshere';
chdir($MyDir) || die "Unable to open $MyDir: $!";
opendir(MYDIR,"$MyDir") || die "Unable to open $MyDir: $!";
my $MyFile ;
my @TI = ();
my $TimeInfo = [EMAIL PROTECTED];
my $MyCTime ;
my %MFTS = ();
my $MyFilesToSort = \%MFTS;
my $MyTimePrt ;

while ( 1 ) {
   $MyFile = readdir(MYDIR);
   last if ( ! defined $MyFile );
   next if ( $MyFile =~ /\.{1,2}$/ );
   next if ( $MyFile !~ /daily\s+mailer$/i );
   if ( -e $MyFile ) {  # want only files
       $MyCTime = (stat($MyFile))[10];
       get_time( $TimeInfo, $MyCTime);
       $MyTimePrt = sprintf "%04d%02d%02d", $TimeInfo->[5]+1900,
                                            $TimeInfo->[4],
                                            $TimeInfo->[3];
       
       $MyFilesToSort->{$MyFile}[0] = $MyCTime;         # epoch seconds when created
       $MyFilesToSort->{$MyFile}[1] = $MyTimePrt;       # yyyymmdd
    }
 }
foreach my $MyKey (sort { $b->[1] <=> $a->[1] } map {[ $_, $MyFilesToSort->{$_}[0] ]} 
keys %{$MyFilesToSort}) {
    printf "%10.0f %-8s %-s\n", $MyKey->[1], $MyFilesToSort->{$MyKey->[0]}[1], 
$MyKey->[0];
 }


sub get_time {
    my ( $TimeInfo, $MyUseTime ) = @_;
    $diff = 86400 * $diff;
#    0    1    2     3     4    5     6     7     8
#   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - $diff);
#   9 => YearModulo
#  10 => Time used in calculations
#
    my $MyPointInTime = time - $diff;
    $MyPointInTime = $MyUseTime if ( defined $MyUseTime );
    @{$TimeInfo} = localtime( $MyPointInTime );
    $TimeInfo->[4]++;
    $TimeInfo->[9] = $TimeInfo->[5] % 100; # Year Modulo, last two digits of year
    $TimeInfo->[10] = $MyPointInTime;
    
    $diff = 0;

 }  # end of get_time
 
        Just a start for you.

Wags ;)


**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to