Very similiar to the Real-time IE/opera "my facorites" shown as links on site , this snippet allows you to place a list of files in a folder and all folders under it, as a links on your site via <#--Include virtual="...">.
use CGI qw(:all); use strict; print header; my $mp3dir="C:\\My Documents\\My Music"; #change this to your filedir my @subs=&findfiles($mp3dir); sub findfiles { my ($currdir)=@_; opendir(MP3ROOTDIR, $currdir); my @allfiles=grep(!/^\.\.?$/, readdir MP3ROOTDIR); closedir(MP3ROOTDIR); my @subdirs=grep(!/.*\..*/, @allfiles); my @rootfiles=grep(/.*\.mp3/i, @allfiles); #change extension to what +ever you want... $currdir=~/^.*\\(.*)$/; print "$1"; foreach (@rootfiles) { my $file=substr($_, 0, -4); #cuts the .mp3 extension... print "<A href=\"$currdir\\$_\"> $file <\/a> <P>"; } foreach (@subdirs) { &findfiles("$currdir\\$_"); } return @subdirs; }

Replies are listed 'Best First'.
Re: file (mp3) list shown on website as links
by ichimunki (Priest) on Dec 23, 2000 at 20:45 UTC
    Or you could be lazy and let Find::File do all the recursion and hard work.
    use strict; use File::Find; #path can be relative to script dir or absolute path my $MP3ROOT = 'my_music'; #my $MP3ROOT = 'C:\\My Documents\\My Music'; #regex is case insensitive my $MP3SUFFIX = '.mp3'; my $PREFIX = 'http://www.homepage.org/'; #my $PREFIX = 'C:\\My Documents\\'; @ARGV = @ARGV || ( $MP3ROOT ); find( sub { if ( ( $File::Find::name =~ /$MP3SUFFIX$/i ) and ( $File::Find::dir !~ /^\./ ) ) # discard .hidden dirs { print "<a href=\"$PREFIX$File::Find::name\">$_</a>\n"; } } , @ARGV );
    Someone correct me if I'm wrong but this eliminates the need to keep track of operating system dependent file delims, too, doesn't it?
      Actually, I've tried using File::Find at first, however, I'm a newbie to perl, and despite using many resources ranging from help from monks, perldoc, and Sams books (Yes, I know, I'll buy the Camel 3rd edition soon...), I just didn't understand how to use it. Thanks, your way is obviously superior. I'll vote ++ on it when I get my votes back (done for the day. :) ) -Nimster Freelance writer for GameSpy industries LTD. (www.gamespy.com)
        The Camel (3rd) is usually great. Perldoc is usually quite informative. But in the case of File::Find, they are both sorely lacking. There isn't much to File::Find, and even that is sparsely explained in every source I checked. I had to do a lot of learning myself to get that script together-- I'd never used the module before either.