Re: File list by date
by kilinrax (Deacon) on Dec 21, 2000 at 18:29 UTC
|
Use 'sort' with a file test operator ('-M' for modification time, '-A' for access time, et.c.).
The following script this works fine on my linux box; apologies if it doesn't port to Windows ;-}
#!/usr/bin/perl -w
use strict;
chdir '/winD' or die "Could not change directory: $!";
opendir DIR, '.' or die "Could not open directory: $!";
my @files = readdir DIR;
closedir DIR;
my @sortedfiles = sort { -M $a <=> -M $b } @files;
$, = "\n";
print @sortedfiles;
| [reply] [d/l] |
Re: File list by date
by mirod (Canon) on Dec 21, 2000 at 18:45 UTC
|
#!/bin/perl -w
use strict;
opendir(ROOT, ".") or die "Arghh!!! $!";
my @sorted_files=
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [ -M $_, $_] }
readdir( ROOT);
print join "\n", @sorted_files;
exit;
| [reply] [d/l] |
Re: File list by date
by davorg (Chancellor) on Dec 21, 2000 at 18:32 UTC
|
opendir(ROOT, $dir) || die "$!\n";
foreach (sort { -M $b <=> -M $a } readdir(ROOT) ) {
print "$_\n";
}
Update: snax below quite rightly points out a bug
in this code.
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [d/l] |
|
Careful! Checking the description of readdir it
says one must either chdir or prepend the $dir
if you want to do filetests.
foreach (sort {-M qq($dir/$b) <=> -M qq($dir/$a)} readdir(ROOT)) {
print $_, $/;
}
for example.
| [reply] [d/l] |
|
Using this method (snax), is there a way to only list
files that are one hour old, or two hours old, etc.?
| [reply] |
|
Re: File list by date
by Fastolfe (Vicar) on Dec 21, 2000 at 19:24 UTC
|
What is the ($files >= 1) bit for? You're putting a string (the filename) into a numeric context here? Are your filenames numeric or are you trying to test to see if $files is non-empty? If it's the latter, consider just doing if ($files) { ... }, but realize that your while statement above that would have done that test itself, or else you wouldn't be inside the loop! The plural itself is kind of a misnomer as well, but it's not hurting anything.
In addition, expanding upon your 'while' test, if you have a filename of 0 (zero), your loop will exit, even if there are more files to be read. Consider checking for defined-ness: while (defined ($file = readdir(DIR))) { ... } | [reply] [d/l] [select] |
Re: File list by date
by I0 (Priest) on Dec 21, 2000 at 18:55 UTC
|
#or
for $files(
map{ unpack'x4a*',$_ }
sort
map{pack 'Na*',(stat "x:/$_")[9],$_}
readdir(root)
){
print "$files\n";
}
| [reply] [d/l] |
Re: File list by date
by jeroenes (Priest) on Dec 21, 2000 at 20:21 UTC
|
Just for fun, let's try to do this with Supersplit, which
can be found here. (It's not
yet in CPAN ;-)).
use SuperSplit;
if ( $^O =~ /linux/){
$dir = `ls -l`; $col = 4;
}else{ #sorry, no macs ;-)
$dir = `dir -w`; $col = 2;
}
@$sortedref =
sort{ $a->[$col] <=> $b->[$col] } @{supersplit('\s+','\n',$dir)};
print superjoin( $sortedref );
I tested it and it works under linux. Would be neater without the
OS-checking, though.
Have fun with it
Jeroen
I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ) | [reply] [d/l] |
|
Heh -- non Perl advice follows :)
If you're using linux (or most any *nix -- I think
this flag is the same for most versions of ls) you
should just use the -t flag and let ls
do the time sorting for you. Or use -T for
most recent first.
Similarly, there are flags for dir that allow
it to do the sorting, too. See dir /? for
clues.
| [reply] |
|
Thanks for this thread. It's got me part of the way through a dilemma I'm trying to figure out. Can anyone tell me how I might add something like --
"if any of these files is/are older than 5 minutes, create a file named warning_x in folder_x"?
Thanks for any input.
Dave
| [reply] |