Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Reading entire subfolder tree into an array

by sdyates (Scribe)
on May 28, 2002 at 16:35 UTC ( [id://169821]=perlquestion: print w/replies, xml ) Need Help??

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

It is amazing how such a simple concept is so difficult to code. I have tried many ways of processing each file with in a directory tree, but each way leads me to a massive problem. I know there must be a simple command or loop to do this. There are many threads here, but none seem to answer the question:

How do I read the entire contents of a subfolder tree into an array. /home/user1 would consist of:
home.txt folder1 test.txt folder2 test2.txt file2.org The array should read the entire listing just as dir would generate and dump it into the array, then take each folder and generate another object list, dumping it into an array, and so on...

Is there not a simple command for doing this? Currently, I have a lot of code and much more to generate.

Please advise,
Simon
  • Comment on Reading entire subfolder tree into an array

Replies are listed 'Best First'.
Re: Reading entire subfolder tree into an array
by broquaint (Abbot) on May 28, 2002 at 16:48 UTC
    If you mean a straight directory listing then just use opendir() in conjunction with readdir()
    opendir(DH, ".") or die("ack - $!"); my @listing = readdir(DH); closedir(DH); print "@listing\n"; __output__ . .. foo bar baz quux
    If you mean a recursive directory listing then you can probably use File::Find to suit your needs.
    HTH

    _________
    broquaint

Re: Reading entire subfolder tree into an array
by SuperCruncher (Pilgrim) on May 28, 2002 at 16:48 UTC
    Well, here's an idea:
    my $dir = '/home/user1'; my @files = glob("$dir/*"); # Get list of files my @directories = grep { -d } @files; # Use -d to get dirs
    Then you could do:
    foreach my $directory (@directories) { my @list = glob("$directory/*"); }
    ...and of course repeat the process until a directory contains no more directories. Probably an easier way of traversing a directory structure is using the File::Find module though it does have some very irritating behaviours... It's a "standard" Perl module, and it should be on your system already.
      Actually I strongly advise using File::Find instead. There's too much to think of when you roll your own: what happens if you run into symlinks for example? Perl standard and CPAN modules for seemingly simple tasks are there for a reason.

      Makeshifts last the longest.

        Based on all your input, I have spent the last few hours readin up on find::file and believe it is the best way to go. However, i do have one remaining question.

        First, herre is my code I have come up with:

        use File::Find; finddepth (\&RetrieveAll, "c:\\fpx"); sub RetrieveAll { push (@list,$_,"\n"); }

        How do I get $_ to include the absolute path of the file? $_ lists the folder name, then all files within it... i'd rather have the absolute path.

        Is this simple?

        Please advise
        Simon
Re: Reading entire subfolder tree into an array
by dorko (Prior) on May 28, 2002 at 21:23 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found