Re: Need a list of files in a dir.
- From: brighttrue@xxxxxxxxx (Bright True)
- Date: Tue, 27 Sep 2005 14:39:24 +0300
Hello ,
in your examples you're opening "files" not dir's , to open a directory use
this instead
opendir(DIR,$dirname) or die $!;
my @content = readdir(DIR);
closedir(DIR);
then to read files inside it ,
for (@content){
print ;
}
that's it , if you need to remove ("." and "..") which means the top
directorys most of time
use it as follow :
opendir(DIR,$dirname) or die $!;
my @content = grep {! /^\./},readdir(DIR);
closedir(DIR);
then again read them using
for (@content){
print;
}
That's it
HTH
On 9/26/05, Tom Allison <tallison@xxxxxxxxxxx> wrote:
>
> Wiggins d'Anconia wrote:
> > Please bottom post...
> >
> > Daniel Kurtz wrote:
> >
> >>Ooh ooh ooh! One I know!
> >>
> >>open(COMMAND, "dir |");
> >>@files = <COMMAND>;
> >>
> >
>
> Well, there's two methods that I use where I can and they are probably
> more portable.
>
> glob works well most of the time:
> @files = </directory/goes/here/*>;
> But it has problems with large numbers of files. And the problems will
> cause it to fail badly.
>
> The other is to opendir and read in a loop.
>
> In either case you need to remove the '.' and '..' entries:
>
> @files = grep {! /^\./} </directory/goes/here/*>
>
> or something like that.
>
> I should warn you, not only is this untested code, but I haven't even
> had my coffee yet.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
> For additional commands, e-mail: beginners-help@xxxxxxxx
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
- References:
- Re: Need a list of files in a dir.
- From: Tom Allison
- Re: Need a list of files in a dir.
- Prev by Date: Re: hi
- Next by Date: PERL's equivalent to an "include file"
- Previous by thread: Re: Need a list of files in a dir.
- Next by thread: Re: the time a program runs
- Index(es):
Relevant Pages
|