Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm working with win2000 server and apache ! My question is: I would like to read the names of all the files in a directory into an array so that I can convert each file name into a link to that file. Thank you Claudia

Replies are listed 'Best First'.
Re: read and link !
by davorg (Chancellor) on Oct 29, 2002 at 11:27 UTC
Re: read and link !
by broquaint (Abbot) on Oct 29, 2002 at 11:32 UTC
    use IO::Dir; my $d = IO::Dir->new('.') or die("ack: $!"); print $_,"\n" for grep -f, $d->read;
    That should give you something to work with. Read the IO::Dir docs for more info on the module.
    HTH

    _________
    broquaint

Re: read and link !
by BrowserUk (Patriarch) on Oct 29, 2002 at 11:54 UTC

    It'll need a little work to adapt it to your environment, but it does most of what you asked for. Come back with your code when you've made your adaptions if you have any problems

    perl -MCGI=:standard -e"print header, html({-title=>'My Files'}, map{ +a({href=>$_},$_).br().$/} <$ARGV[0]>);" *

    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: read and link !
by roik (Scribe) on Oct 29, 2002 at 12:06 UTC
    Try :
    use strict; my $dir = "c:\\temp\\"; opendir (DIR, $dir) or die "Unable to open dir $dir : $!"; my @files = readdir(DIR) or die "Unable to readdir $dir : $!"; closedir DIR; for (@files) { print "$_\n" }
    Substituting $dir for your pathname!
      my $dir = "c:\\temp\\";

      For the benefit of any who might not know: You don't need to use backslashes when running on a Windows machine; Perl will do the right thing and convert a forward-slash path delimiter to the appropriate delimiter for whatever platform it's running on. That way you don't have to backslash escape your backslashes. You can just write:

      my $dir = "c:/temp/";

      which is just a little easier and less error prone, not to mention makes your programs a little less platform-specific. : )

      Jon

        Actually, it's not perl that does it. Forward slashes are recognized by Windows as equivalent to backslashes for purposes of path delimiting. It's only certain of the windows-based programs -- including the braindead command shell! -- that insist that forward slashes are only for indicating command-line options.
        Thanks for the tip!
        I can see it is useful in this and most other cases, but would it cause a problem if I wanted to concatenate $dir back with $_ and use it in a system call to DOS?
        i.e. Perl likes /, but does DOS?
      thank you for your help, but now how can create a link in my html page with @files ??? example: the folder is c:\temp the file is pippo.tif how can create ??? thank you claudia
        i'm sorry but i have lost a part of question: example: the folder is c:\temp - the file is pippo.tif. I need to create a href ..... of all files in $dir how can create ??? thank you claudia
        Try replacing the print statement above with :
        print "<a href=\"$dir$_\">$_</a>\n"
        I wonder if that is what you mean?