Solved

How to make a list of xth latest files in a dir and sub-dir?

Posted on 2003-11-06
5
249 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:irlrobins
  • 4
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 9698520
use CGI qw/:standard/;
use File::Find;
@dirstosearch=("/path/to/dir");
find( sub{push@list,[-M,$File::Find::name] }, @dirstosearch);
print header,start_html("list of 20 latest files")
for( (sort $a->[0]<=>$b->[0],@list)[0..19] ){
    print qq(<a href="http://mysite.com/$_->[1]">$_->[1]</a><br>);
}
print end_html;
0
 

Author Comment

by:irlrobins
ID: 9698614
Thanks ozo. I'll try it out tomorrow and if it works the points are yours!
0
 

Author Comment

by:irlrobins
ID: 9700562
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
0
 

Author Comment

by:irlrobins
ID: 9700600
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....
0
 

Author Comment

by:irlrobins
ID: 9702627
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$/)||(/\.jpg$/))) #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;
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now