The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Sort Directory and list files based on date and time
Discuss Sort Directory and list files based on date and time in the Perl Programming forum on Dev Shed. Sort Directory and list files based on date and time Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 10th, 2006, 09:30 AM
|
|
Contributing User
|
|
Join Date: Oct 2005
Posts: 46
Time spent in forums: 12 h 11 m 44 sec
Reputation Power: 8
|
|
|
Sort Directory and list files based on date and time
hi all,
I have to look for a directory and filter all CSV files. Then sort those based on the time they were created. I need to get the latest file then for further processing. I tried with 'sort', which does sorts, but sorts for the file name instead.
your great ideas are welcome.
Code:
@files = get_CSV_Files($folderName);
foreach (@files) {
print "\n$_";
}
sub get_CSV_Files {
my $dir = shift;
my $dh = DirHandle->new($dir) or $val=3; #die "can't opendir $dir: $!";
return sort # sort pathnames
grep { -f } # choose only "plain" files
map { "$dir$_" } # create full paths
grep { m/.csv/i } # filter csv files
$dh->read(); # read all entries
}
Thanks
sath.
|

January 10th, 2006, 10:37 AM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
|
you can create a hash where the keys are the filenames and the creation date is the value, then sort the hash by the values.
|

January 10th, 2006, 11:00 AM
|
|
Contributing User
|
|
Join Date: Oct 2005
Posts: 46
Time spent in forums: 12 h 11 m 44 sec
Reputation Power: 8
|
|
Quote: | Originally Posted by KevinADC you can create a hash where the keys are the filenames and the creation date is the value, then sort the hash by the values. |
Good idea, Kevin
I will try it. Thanks
|

January 10th, 2006, 12:02 PM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
sticking with your code, maybe something like this will work:
Code:
my %files = get_CSV_Files($folderName);
foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
print "$keys\t", scalar localtime($files{$keys}), "\n";
}
sub get_CSV_Files {
my $dir = shift;
my $dh = DirHandle->new($dir); #die "can't opendir $dir: $!";
return map {$_ => (stat($_))[9]}
map { "$dir$_" }
grep { m/.txt/i }
$dh->read();
}
|

January 10th, 2006, 12:53 PM
|
|
Contributing User
|
|
Join Date: Oct 2005
Posts: 46
Time spent in forums: 12 h 11 m 44 sec
Reputation Power: 8
|
|
Quote: | Originally Posted by KevinADC sticking with your code, maybe something like this will work:
Code:
my %files = get_CSV_Files($folderName);
foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
print "$keys\t", scalar localtime($files{$keys}), "\n";
}
sub get_CSV_Files {
my $dir = shift;
my $dh = DirHandle->new($dir); #die "can't opendir $dir: $!";
return map {$_ => (stat($_))[9]}
map { "$dir$_" }
grep { m/.txt/i }
$dh->read();
}
|
Kevin,
Excellent.
Thanks
Sath.
|

January 10th, 2006, 03:53 PM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
|
hehehe.... you will want to change the .txt back to .csv of course!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|