Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Script to display files on a web page.

Status
Not open for further replies.

Marty99

Technical User
Aug 1, 2003
8
US
I have a script in ASP that will dynamically create a web page of the files in the current directory and sort them alphabetically.

I want to do this same thing in Linux, ASP will not work, and the converted ASP (asp2php) does not work either.

Any ideas, your help is appreciated.
 
Pretty trivial to do in linux.

Code:
#!/usr/bin/perl
use CGI;
$query = new CGI;
print $query->header ;

opendir(DIR,"./");
@files = grep !/^\./, readdir(DIR);
closedir(DIR);

foreach $file(sort @files){
  print "$file<BR>" ;
}

You can vary this to do different things ,templatize it etc.
 
I'd refine that code slightly (depending upon your requirements):
Code:
#!/usr/bin/perl
use CGI;
$query = new CGI;
print $query->header ;

opendir(DIR,"./");
@files = grep !/^\.+$/, readdir(DIR);
closedir(DIR);

print join("<BR>", sort @files);
The regex in siberian's grep would ignore any file beginning with a '.', not just the directory shortcuts of '.' & '..'. The last line just speeds things up slightly. Though as always there's more than one way to do it :)

Barbie
Leader of Birmingham Perl Mongers
 
Ya, I missed that second dot, I did a quick copy/paste from the man page on the wrong example.

Sorry about that.
 
When i try to run this I get the following, I have perl-5.8.0-88 installed on Redhat 9, I don't have CGI.pm on my system:

[root@NAWRCS-SYSLOG cisco]# ./web.gen
Can't locate CGI.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at ./web.gen line 2.
BEGIN failed--compilation aborted at ./web.gen line 2.
[root@NAWRCS-SYSLOG cisco]#
 
without CGI...

[red]#!/usr/bin/perl

print "Content-type:text/html\n\n";

opendir (LIST, ".");
@list = grep(/[^.][^index.pl]/, readdir (LIST));
close (LIST);

foreach $item (@list) {
print "<li><a href='$item'>$item</a>\n";
}[/red]



Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top