On Thu, May 13, 2004 at 07:19:54PM -0400, [EMAIL PROTECTED] wrote:
> Hello,
> 
> I'm still extremely new to Perl, and was wondering if some
> of you might mind taking a peek at the code below and
> telling me if there's anything that could make it better or
> more efficient?
> 
> The code below is my attempt at grabbing the files from a
> folder and then sorting them by modification date.
> 
> Thanks for your insight.
> 
> Jay
> 
> -----
> 
> #!/usr/bin/perl
> 
> my $dir = "/Users/jay/Desktop/Other Stuff/old stuff 4";
> 
> opendir FOLDER, $dir or die "Cannot open $dir: $!";
> 
> foreach $file (readdir FOLDER) {
>    next if $file =~ /^\./;
>    $path = "$dir/$file"; 
>    next unless -f $path and -r $path;   
>    push @files, (stat $path)[9].chr(1).$path."\n";
> }
> 
> @files = sort @files;
> 
> foreach (@files) {
>    push @sortedList, (split(chr(1), $_))[1];
> }
> 
> print @sortedList;

Jay,

This is a bit off-topic as your question isn't specific to MacOS and
Perl;  however, I can't resist a challenge nor the opportunity to help
you learn Perl, so here's my offering:

#!/usr/bin/perl

use Cwd;
use File::Find;

find( sub { -f $_ && -r _ && push @files, $File::Find::name }, shift || cwd() );
print join "\n", (sort @files), '';

The File::Find module can be a little cryptic, so you might be
interested to look at Randal Schwartz's "File::Finder" module (not a
standard module, so you'll have to install yourself).  Also,
File::Find will do a recursive search of directories by default, so
you'll have to consult the docs (via "perldoc File::Find").

HTH,

ky

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to