Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how list all the files and directories?

by littlemonk (Sexton)
on Jan 06, 2014 at 18:30 UTC ( #1069560=perlquestion: print w/ replies, xml ) Need Help??
littlemonk has asked for the wisdom of the Perl Monks concerning the following question:

i am trying to list all the directories and files and store them in an array ,but dont why this is not working..where i am doing mistake?plz help thank you!

$path="D:/Chanti"; opendir ( DIR, $path ) || die "Error in opening dir $dirname\n"; while($filename=readdir(DIR)){ next if ($filename =~ m/^\./); if (-f $filename){ push(@file,$filename); print "\n"; }else if (-d $filename){ push(@dir,$filename); print "\n"; } }

its not listing all the directories except ".." and "." remaining all are going as files

Comment on how list all the files and directories?
Download Code
Re: how list all the files and directories?
by LanX (Canon) on Jan 06, 2014 at 18:33 UTC
    what do you think this line is doing?

    next if ($filename =~ m/^\./);

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      tried ignoring that "."

        the code "works" mostly, it's populating the arrays.

        you are just printing empty lines instead of the content of the arrays.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        update

        Plz be aware that you are excluding any file or dir starting with a dot.

        use strict; use warnings; use Data::Dump qw/pp/; my $path="/tmp"; opendir ( DIR, $path ) || die "Error in opening dir $path\n"; my (@file,@dir); while (my $filename=readdir(DIR)) { next if ($filename =~ m/^\./); if (-f $filename) { push(@file,$filename); # print "\n"; } elsif (-d $filename){ push(@dir,$filename); # print "\n"; } } pp \@file,\@dir;

        plz tell me wer im doing wrong

        $path="D:/Chanti"; opendir ( DIR, $path ) || die "Error in opening dir $dirname\n"; #//print "directory contains-->@arr"; while($filename=readdir(DIR)){ print "$filename"; if (-f $filename){ push(@file,$filename); } if (-d $filename){ push(@dir,$filename); } } print "Directory--->@dir\n"; print "\n";print "\n";print "\n";print "\n";print "\n";print "\n"; print "File---->@file\n";
Re: how list all the files and directories?
by Laurent_R (Vicar) on Jan 06, 2014 at 18:47 UTC
    You are storing the files and directories into two arrays, but are never printing them. If you expect your program to output the filenames, then print the content of the @file array (or the filename when you grab it). .
Re: how list all the files and directories?
by toolic (Chancellor) on Jan 06, 2014 at 18:52 UTC
    Post the actual code you are running. Compile error: "else if" should be "elsif".
Re: how list all the files and directories?
by jellisii2 (Pilgrim) on Jan 06, 2014 at 21:11 UTC
    Do yourself a favor and learn to use File::Find. I'm sure merlyn will appreciate this mention.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069560]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-07-24 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls