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

Hi, I am working in Win32 and I want to get the list of file names under the current folder. There will be folders/files within it, folders/sub-folders/files within it. I used the opendir command as follows.

opendir(DIR, $isbn) or print "Can't open $isbn: $! No such job found!" +; my @contents = grep /(.+?)/, readdir DIR; foreach $file ( @contents ) {print "$file\n";}

But, with this, I get a display of only the folders and not the files/sub-folders under it. I want to get the list of all the files which are present within subfolders in the particular folder. Can anyone help me with the code for this?

Replies are listed 'Best First'.
Re: Listing of files in its sub-folders
by GrandFather (Saint) on Aug 31, 2006 at 10:04 UTC
Re: Listing of files in its sub-folders
by shmem (Chancellor) on Aug 31, 2006 at 10:09 UTC
    Your grep is useless in it's current form. You could as well say my @contents = readdir DIR;

    Change that to

    my @contents = grep !/^\.\.?$/, readdir DIR;

    to exclude the current and parent directory entries.

    Then in your foreach loop, test whether a given entry is a directory. See -X. If it is a directory, open it and get the files in there (as you did with the current).

    But you could also use File::Find from CPAN.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}