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

hello i want to list the directories, found files,. Currently i am using following module to find the file

my $foldername; my $subdirlevel= shift // 2 ; my @files = File::Find::Rule->file() ->name('music.xml') ->maxdepth($subdirlevel) ->in( $foldername); print Dumper (@files); this give me the path where the music.xml is but not the directory nam +e.

how do i also get the directory name instead of the path to the file?

Replies are listed 'Best First'.
Re: how to find the file from the subdirectories of a directory?
by toolic (Bishop) on Jun 22, 2012 at 21:25 UTC
Re: how to find the file from the subdirectories of a directory?
by zentara (Cardinal) on Jun 23, 2012 at 09:57 UTC
    Hi, plain old File::Find stores $File::Find::dir. Note that the depth is derived from counting slashes from the full path names.
    Test output:
    $ ./file-find-recursion-depth-subdirs-ext . 6 z.xml /home/zentara/1down/1/1/a/z.xml /home/zentara/1down/1/1/a
    #!/usr/bin/perl # linux only for counting depth, you can adjust for Windows use warnings; use strict; use File::Find; use File::Spec; if (@ARGV < 2){print "Usage: $0 dir depth\n";exit} my ($path, $depth)= @ARGV; # scriptname . 6 would descend 6 layers into current dir's full path # so depth is often gretaer than you think :-) my $abs_path = File::Spec->rel2abs($path); #in case you enter . for di +r my $m = ($abs_path) =~ tr!/!!; #count slashes in top path my @files; my @dirs; find (\&found,$abs_path); print "@files\n"; print "@dirs\n"; exit; sub found{ my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if $n > ($m + $depth); # do stuff here. if( -f && $File::Find::name=~/\.xml$/){ push @files, $_; #name only push @files, $File::Find::name; #name with full path push @dirs,$File::Find::dir; # dir full path } }

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