Gossamer Forum
Home : General : Perl Programming :

Getting a folder listing....

Quote Reply
Getting a folder listing....
I was wondering if there is a way to get a directory listing of a folder, and then put it into an array?

Is that possible?

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
See:

perldoc -f opendir

and

perldoc -f readdir

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Getting a folder listing.... In reply to
In that case, should this work;

Code:
#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);

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

$somedir = "$ENV{DOCUMENT_ROOT}" . "/development/";

print $somedir;

opendir(DIR, $somedir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

foreach $line (@dots)
{

print $line;

}
I tried it, and all that was show is the $somedir variable (which i printed earlier to see if the path was correct).

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
Code:
opendir(DIR, $somedir) || die "can't opendir $some_dir: $!";
@files = grep { !/^\.{1,}$/ } readdir(DIR);
closedir DIR;
Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: Getting a folder listing.... In reply to
Thanks. Now I'm trying to work out how to seperate these listings Wink

Is there a way to add a delimiter between the names? Maybe a || or :: ???

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
Just do

print "$_<BR>" foreach @array_name;

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: Getting a folder listing.... In reply to
Yeah, but the name is just printed as one long string. Would the above code work for that?

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
There was a br tag but the forum messed it up........the code above will print it in a list not a long line.

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: Getting a folder listing.... In reply to
Mmm..adding that code you suggested gives me an IS Error Frown

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
It isn't my code giving the error. You probably messed up somewhere or didn't print a header.

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: Getting a folder listing.... In reply to
#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);

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

$somedir = $ENV{DOCUMENT_ROOT} . "/development/";

opendir(DIR, $somedir) || die "can't opendir $some_dir: $!";
@list = grep { !/^\.{1,}$/ } readdir(DIR);
closedir(DIR);

print "$_<BR>" foreach @list;

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Getting a folder listing.... In reply to
Try changing die to something a little more user friendly. That may shed some light Smile (or check your error log)

Make sure @list isn't empty too....if ($#list > -1)

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: Getting a folder listing.... In reply to
In Reply To:
It isn't my code giving the error.
Fraid so, that code doesn't work on older perl's:

[alex@penguin lsqldev]$ perl5.00404 -e 'print "$_<BR>" foreach @list;'
syntax error at -e line 1, near ""$_<BR>" foreach "
Execution of -e aborted due to compilation errors.
[alex@penguin lsqldev]$

Try:

foreach (@list) { print "$_<BR>"; }

instead.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Getting a folder listing.... In reply to
Well it depends what version of perl he was using.....Smile

It works with version 5.005



Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/