Andrek
December 10, 2006, 9:02pm
1
Perl file::find can I sort the out put
I am using file::find in my script but how I wish to process each file found in date order.
Can I sort this module?
eg
part of current script is....
use File::Find;
# Recursively find all files and directories in $mqueue_directory
find(\&wanted, $mqueue_directory);
my $nj=0;
sub wanted {
$nj++;
# print "In the wanted routine $nj $_\n";
# Is this a qf* file?
if ( /^qf\w/ ) {
etc
etc...
}
Just make sure you capture the size of each file you want (together with the filename) in the wanted function and dump them to an array. Then you can post-process the list using sort().
Here is an example:
#!/usr/bin/perl -w
use File::Find;
my @files = ();
find(sub {
if ($_ =~ /\.mp3$/) {
push(@files, [
$File::Find::dir . "/$_",
(stat($_))[7]
]);
}
}, "/home/bernardchan/scratch");
map {
print "$$_[1]\t\t$$_[0]\n"
} sort {
$$a[1] <=> $$b[1]
} @files;
Sample output:
3583250 /home/bernardchan/scratch/music/Heart - Alone.mp3
3790439 /home/bernardchan/scratch/music/S Club 7 - Dancing Queen.mp3
4003132 /home/bernardchan/scratch/music/bwv541.mp3
4272925 /home/bernardchan/scratch/music/toccata+fugue-bwv565.mp3
4276686 /home/bernardchan/scratch/music/toccata-bwv540.mp3
4434960 /home/bernardchan/scratch/music/Barry Manilow - The Old Songs.mp3
5840063 /home/bernardchan/scratch/music/j.s.bach-fantasia+fuga-bwv542.mp3
Andrek
December 12, 2006, 12:19am
3
Thanks, - I give it a go....
Andrek