Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Finding directories at a variable depth

by uday_sagar (Scribe)
on Aug 01, 2012 at 11:28 UTC ( #984769=perlquestion: print w/ replies, xml ) Need Help??
uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I think this is one of already asked questions!

I want the script to list all the directories that are at some depth which will be specified by the user.

The code I have is finding all the directories recursively. I want only at a particular level.

use Cwd; use File::Find; my $dir = getcwd; find(\&print_name_if_dir, "$dir"); sub print_name_if_dir { if (-d) { print $_."\n"; } }

Thanks

Comment on Finding directories at a variable depth
Download Code
Re: Finding directories at a variable depth
by Ratazong (Prior) on Aug 01, 2012 at 11:36 UTC

    You could have a look at $File::Find::dir, which gives the directory where $_ is found; there you can get the depth by counting the number of parts after doing a split using the seperator (/).

    HTH, Rata
Re: Finding directories at a variable depth
by RichardK (Priest) on Aug 01, 2012 at 11:56 UTC

    You could just use glob, if you construct the right match string

    my @files = glob('/*/*/*');

    That will give you all the files like

    /a/b/c /a/b/d /a/x/a ...
Re: Finding directories at a variable depth
by nemesdani (Friar) on Aug 01, 2012 at 12:03 UTC
Re: Finding directories at a variable depth
by daxim (Chaplain) on Aug 01, 2012 at 12:23 UTC
Re: Finding directories at a variable depth
by zentara (Archbishop) on Aug 01, 2012 at 14:46 UTC
    Here is a File::Find script which counts slashes to determine depth. Modify the depth and slash according to your needs.
    #!/usr/bin/perl # linux only, use appropriate \ for MSWindows use warnings; use strict; use File::Find; use File::Spec; if (@ARGV < 2){print "Usage: $0 dir depth\n";exit} my ($path, $depth)= @ARGV; my $abs_path = File::Spec->rel2abs($path); #in case you enter . for di +r my $m = ($abs_path) =~ tr!/!!; #count slashes in top path find (\&found,$abs_path); exit; sub found{ my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if $n > ($m + $depth); return unless -d; # do stuff here. #print "$_\n"; #name only print "$File::Find::name\n"; #name with full path }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      This is just what I wanted zentara!

      Thanks.

        And if i wanted only directories of level 2 and not of level 1?

Re: Finding directories at a variable depth
by uday_sagar (Scribe) on Aug 02, 2012 at 13:18 UTC

    I have played with your code for some time and got one similar to that posted above!

    #!/usr/bin/perl # linux only, use appropriate \ for MSWindows use warnings; use File::Find; use File::Spec; use Cwd; my $total_count; my $pwd = getcwd; my $count = ($pwd =~ tr/\///); if (@ARGV < 2){print "Usage: $0 dir depth\n";exit} my ($path, $depth)= @ARGV; my $abs_path = File::Spec->rel2abs($path); #in case you enter . for di +r #count slashes in top path my $m = ($abs_path) =~ tr!/!!; find (\&found,$abs_path); print "\n\nTOTAL: $total_count\n"; exit; sub found { my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if $n > ($m + $depth); return unless -d; # do stuff here. my $count_here = ($File::Find::name =~ tr/\///); if ($count_here eq $count+2) { print "$_\n"; $total_count++; #name only } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-08-29 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls