Perl Get Parent Folder Name

What is the solution to get the name of the parent directory using File::Find. I know how to get only the filename or only the directory path but I don't know how to do this for the last containing directory.

For example, if the directory is /dir_1/dir_2/dir_3/.../dir_n/*.txt I need to get the 'dir_n' name.

use strict;
use warnings;
use File::Find;

my $dir = "some_path";

find(\&file_handle, $dir); 
sub file_handle {
    /\.txt$/ or return;
    my $fd = $File::Find::dir;
    my $fn = $File::Find::name;
    # ...
}

Answers


Given the directory path, you then apply File::Basename (another core module) to the path to obtain the last portion of the directory.

use strict;
use warnings;
use File::Find;
use File::Basename;

my $dir = "some_path";

find(\&file_handle, $dir); 
sub file_handle {
    /\.txt$/ or return;
    my $fd = $File::Find::dir;
    my $fn = $File::Find::name;
    my $dir = basename($fd);
    # ....
}

Need Your Help

Non Square Matrix Multiplication Help C++

c++ matrix vector multiplication

first off sorry for the slightly messy code as I was fiddling with different things to try to get it to work. As of now, my code can multiply Square Matrices just fine; however, it has difficulties

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.