SPUG: Win32 Sorting directory by date

Chris Wilkes cwilkes at ladro.com
Wed Oct 3 12:24:41 CDT 2001


On Wed, 3 Oct 2001, Bryan Dees wrote:

> Thanks for the followup's everyone. Your solutions work perfectly under
> Unix. 
> However, the provided solution doesn't sort files under Microsoft's
> 'challenged' NT OS:
> 
>     opendir(DIR, ".");
>        @FILES = sort { -M $a <=> -M $b } readdir(DIR);
>        print "The files you requested:\n". join("\n", @AllFiles2) ."\n";
>     close(DIR);

Two things

1) you say "@AllFiles2" when you probably meant to say @FILES.
2) you should modify your script to check for files outside of the current
directory by doing a map to pass along the directory name.  Then use
File::Basename to get just the file name out of the full path.

Looks like it worked for me with the oldest file appearing last.

#!c:\perl\bin\perl.exe

use File::Basename;
use strict;
my ($dir, @files);

$dir = shift || die "Pass me a directory\n";
opendir(DIR, $dir) || die "Can't open directory '$dir'\n";
  @files = map { basename($_) } sort { -M $a <=> -M $b } map { "$dir/$_" }
readdir(DIR);
close(DIR);

print "The files you requested:\n" . join("\n", @files) . "\n";


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list