irlrobins
asked on
How to make a list of xth latest files in a dir and sub-dir?
I'm looking for a script that will search through a dir and its subdirs and return in a list (html file), the 10 or 20 most recent files, according to last modified timestamp (regardless of file type). Preferably the list will have a link to each of the files (the dir is a sub dir of my htdocs folder). I'm running Apache 2.0.48 on Win XpPro and have perl 5.6.1 installed. No real experience with perl so don't get too techie on me!!! ;-)
Thanks Robin
Thanks Robin
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I tried that and i get the a Server 500 error. Here's whats in the error logs:
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] Premature end of script headers: list.cgi
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] syntax error at C:/Program Files/Apache Group/Apache2/cgi-bin/list .cgi line 8, near "){"
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] Execution of C:/Program Files/Apache Group/Apache2/cgi-bin/list .cgi aborted due to compilation errors.
Line 8 is "for( (sort $a->[0]<=>$b->[0],@list)[0 ..19] ){"
Whats the prob?
Thanks again
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] Premature end of script headers: list.cgi
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] syntax error at C:/Program Files/Apache Group/Apache2/cgi-bin/list
[Fri Nov 07 12:09:30 2003] [error] [client xxxx] Execution of C:/Program Files/Apache Group/Apache2/cgi-bin/list
Line 8 is "for( (sort $a->[0]<=>$b->[0],@list)[0
Whats the prob?
Thanks again
ASKER
Ok i added a ';' to the end of line 7 "print header,start_html("list of 20 latest files")"
Script now runs and returns a list of 20 files. But they're not the most recent ones. In fact I've no idea why it picks the files that it does....
Script now runs and returns a list of 20 files. But they're not the most recent ones. In fact I've no idea why it picks the files that it does....
ASKER
ok I've fixed it. I've changed the ozo's solution slightly:
a) to fix his errors (haven't you heard of testing?? ;-) )
b) it now only lists files that end in .txt and .jpg and no directories), Also using substr so i can formulate a proper url (it was adding extra dir's to the path)
Anyway Ozo's points are yours. Thanks for your help. Appreciate it. (solution follows)
#!/usr/bin/perl -w
use CGI qw/:standard/;
use File::Find;
@dirstosearch=("../htdocs/ access/");
find ( sub
{
if ((!-d)&&((/\.txt$/)||(/\.j pg$/))) #will not push dir's or files that don't end with txt or jpg to the array
{
push@list,[-M,$File::Find: :name]
}
}, @dirstosearch);
print header,start_html("The 20 most Recent Files");
for( (sort {$a->[0]<=>$b->[0]}@list)[ 0..19] ){
$substring1=substr($_->[1] ,9); #substring to from correct url
$substring2=substr($_->[1] ,19); #substring to form visible link in correct format
print qq(<a href="http://mysite.ie$substring1">$substring2</a><br >);
}
print end_html;
a) to fix his errors (haven't you heard of testing?? ;-) )
b) it now only lists files that end in .txt and .jpg and no directories), Also using substr so i can formulate a proper url (it was adding extra dir's to the path)
Anyway Ozo's points are yours. Thanks for your help. Appreciate it. (solution follows)
#!/usr/bin/perl -w
use CGI qw/:standard/;
use File::Find;
@dirstosearch=("../htdocs/
find ( sub
{
if ((!-d)&&((/\.txt$/)||(/\.j
{
push@list,[-M,$File::Find:
}
}, @dirstosearch);
print header,start_html("The 20 most Recent Files");
for( (sort {$a->[0]<=>$b->[0]}@list)[
$substring1=substr($_->[1]
$substring2=substr($_->[1]
print qq(<a href="http://mysite.ie$substring1">$substring2</a><br
}
print end_html;
ASKER