Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You're looping on the first item in the list which will be a '.'

Works better if you add next if $item eq '.' or $item eq '..'; after the foreach

readdir gives you the names in the directory... not path names, so when you call the printdir recursively you'll need to prefix the current directory to each element of the list. (Naturally you'll need to change your '.' test accordingly.

Anyway this seems to work on my machine.

#!perl -w
use strict;
use Carp;

printdir(@ARGV);

sub printdir {
    my $item;
    foreach $item(@_) {
      next if $item =~ /\.{1,2}$/;
      if (-d $item) {
        print "$item\n";
        opendir(SUBDIR, $item) or croak "Can't open directory :$!";
        my @subdir_items = readdir(SUBDIR);      #+
        closedir(SUBDIR);                        #+  
        printdir(map {"$item/$_"}@subdir_items); #+
      }
    }
  }

Oh, by the way the equivalent in my previous post should have been

perl -Mstrict -MFile::Find -wle 'find sub{print $File::Find::dir if -d},@ARGV' .
but you saw that already, didn't you :)

Update: Just saw merlyn's post. The above ran on Win32 because (as he says) the Windows port recognizes forward slashes as valid directory separators. I don't know whether this is an issue on other non unix ports. Still, I agree with him, it's a lot safer here to stay with the standard.

Update 2: The '.' test is broken: it will match any file ending with a '.'. You'd need to split off the file name from the path, so you might as well use File::Spec... Oh good grief, why did I ever post this code? This'll teach me to shut up :(


In reply to Re: As usual by Tyke
in thread Recursive directory scanning by pwhysall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found