Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: listing all subdirectories of directory into file

by NetWallah (Abbot)
on May 25, 2012 at 21:47 UTC ( #972543=note: print w/ replies, xml ) Need Help??


in reply to listing all subdirectories of directory into file

Show us what you have tried, what you expected it to do, and how you think it failed. This will give you (and others attempting to learn from this site) a better experience.

A slightly simpler approach than jdrago999's above is to use these built-in perl functions to achieve your objective:

opendir, readdir, closedir.

You will need to use the "-d" and "-f" operators to distinguish between directory names and file names. See -X for information on this.

             I hope life isn't a big joke, because I don't get it.
                   -SNL


Comment on Re: listing all subdirectories of directory into file
Re^2: listing all subdirectories of directory into file
by sauoq (Abbot) on May 25, 2012 at 22:17 UTC
    A slightly simpler approach than jdrago999's above is to use these built-in perl functions to achieve your objective:

    Do you think? I think File::Find would be the simpler approach... it does a lot for you. I'd do it something like this (untested):

    #!/usr/bin/perl use warnings; use strict; use File::Find; use Cwd qw(abs_path); sub w { -d && print $File::Find::name,$/ } File::Find::find( {wanted=>\&w}, abs_path($_)) for @ARGV;

    -sauoq
    "My two cents aren't worth a dime.";
      I would agree that File::Find is more useful, more powerful, and more productive.

      However, it has some requirements/expectations of the user. It requires the user to understand callbacks, depth-first vs. width first, How to get the current directory name - all of which you can get if you RTFM, but IMHO, it is conceptually fairly difficult to ingest, for a newbie. That was my reasoning behind recommending 'opendir'. Also, once someone has gone through that pain, it is easier to appreciate a fine module like FF and FF:Rule.

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

        It requires the user to understand callbacks, depth-first vs. width first, How to get the current directory name

        Ah, but if you are going to roll it by hand, you should probably understand depth vs. breadth first searching. And certainly how to get the current directory name. You might also want to know a thing or two about symlinks and symlink loops, and think out your error handling. And, really, if you are going to do all that, there's a good chance you understand callbacks already. (BTW, I don't know how much you really need to understand about callbacks to follow an example and use one; not that I advocate using something that you don't understand... but you gotta learn somehow.)

        -sauoq
        "My two cents aren't worth a dime.";

      At first I thought File::Find might not be that good for this, because he wants to make a list within each directory of its subdirectories, which at first glance means "looking at" each directory twice: once when you're adding it to its parent directory's list, and then again when File::Find comes across it. But then I thought of this, which is very simple, but has the downside of opening the output file every time it needs to write a line:

      #!/usr/bin/env perl use Modern::Perl; use File::Find; sub w { if( -d $_ ){ open my $out, '>>', 'list' or die $!; print $out "$_\n"; close $out; } } File::Find::find(\&w, '.');

      Aaron B.
      Available for small or large Perl jobs; see my home node.

        Why open and close $out every time?

        #!/usr/bin/env perl use Modern::Perl; use File::Find; # $out is used within w(): open my $out, '>>', 'list' or die $!; sub w { if( -d $_ ){ print $out "$_\n"; } } File::Find::find(\&w, '.');

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-12-26 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls